MCP Identity and Secure User Context

Project: Investment Portfolio Tracker

Area: MCP, authentication, authorization, LLM tool calling, API architecture

The Challenge

While building my Investment Portfolio Tracker, I exposed portfolio and document capabilities through an MCP server so that external AI clients could interact with the application.

The difficult part was not exposing the tools themselves. The challenge was determining how the backend could reliably identify the current application user when a request originated from an external MCP client.

I also did not want the LLM to know or provide internal database identifiers such as user_id. Those identifiers are implementation details of the backend and should not be treated as trusted input from an AI model.

Architectural Reasoning

I initially approached the problem as an authentication problem, but realized that it was more fundamentally an identity propagation and trust-boundary problem.

The external AI client needs a way to authenticate as a particular application user, while the backend should remain responsible for resolving that identity and performing authorization.

This led me to separate external authentication from internal database identity. The AI client does not need to know the application's internal user identifier.

Solution

I introduced Personal Access Tokens (PATs). A user can create a PAT from the application with an expiry time and configure that token in their MCP/LLM client.

When the MCP client makes a request, the backend validates the PAT and resolves it to the corresponding application user. The authenticated identity is then carried internally by the backend while executing the requested tool.

This allows a user to ask an AI client something such as:

Connect to my investment portfolio tracker and tell me my current portfolio.

The LLM can discover and call the appropriate portfolio tool without needing to know the user's internal user_id. The backend already knows which authenticated user the request belongs to.

Protecting Internal Resource Identifiers

I encountered a similar problem when exposing user documents through the assistant.

Internally, the database uses a chat_id to identify a conversation. I did not want this internal database identifier exposed to the LLM.

Instead, I introduced a public UUID for the chat and exposed it together with useful metadata such as the chat title and summary.

This created another design problem: if the LLM does not know the internalchat_id, how can it determine which conversation contains documents relevant to the user's question?

I solved this by providing meaningful metadata and the public UUID to the model. The LLM can reason about the available conversations using their titles and summaries, select the relevant public UUID, and provide it through the tool interface.

The backend then resolves that public identifier to the internal database record and performs the required authorization and database operations.

Engineering Principle

The main principle I took from this challenge was to keep external interfaces separate from internal implementation details.

The LLM should interact with stable, meaningful resource references, while the backend remains responsible for authentication, authorization, identifier resolution, and access to internal database fields.

This challenge taught me to think carefully about what information should cross a system boundary, which component should be trusted with that information, and where identity and authorization decisions should happen.