Skip to content

MCP: Support authInfo #422

Closed
Closed

Description

@kentcdodds

Issue: Request Support for authInfo from Transport in McpAgent

Problem Description

Currently, the McpAgent class from 'agents/mcp' does not provide access to
the authInfo from the underlying MCP transport layer. This prevents request
handlers from accessing authentication information that should be automatically
populated by the transport.

Current Behavior

When setting request handlers in McpAgent, the extra parameter in request
handlers does not contain the authInfo property, even though the MCP SDK's
RequestHandlerExtra type defines it as:

export type RequestHandlerExtra<
	SendRequestT extends Request,
	SendNotificationT extends Notification,
> = {
	signal: AbortSignal
	authInfo?: AuthInfo // This is undefined in our handlers
	sessionId?: string
	_meta?: RequestMeta
	requestId: RequestId
	requestInfo?: RequestInfo
	sendNotification: (notification: SendNotificationT) => Promise<void>
	sendRequest: <U extends ZodType<object>>(
		request: SendRequestT,
		resultSchema: U,
		options?: RequestOptions,
	) => Promise<z.infer<U>>
}

Expected Behavior

Request handlers should be able to access authInfo from the extra parameter
when the transport has validated an access token:

this.server.server.setRequestHandler(
	SetLevelRequestSchema,
	async (request, extra) => {
		// extra.authInfo should contain validated token information
		const authInfo = extra.authInfo
		if (authInfo) {
			console.log('Authenticated user:', authInfo.clientId)
			console.log('Token scopes:', authInfo.scopes)
		}
		// ... rest of handler logic
	},
)

Impact

Without access to authInfo from the transport:

  1. Request handlers cannot access authentication context - they must rely on
    agent-level props
  2. Inconsistent authentication patterns - some handlers use
    this.props.authInfo, others can't access transport auth
  3. Limited OAuth integration - can't leverage the transport's built-in token
    validation
  4. Type safety issues - the extra.authInfo property exists in types but is
    always undefined

Proposed Solution

The McpAgent class should be updated to allow me to populate the authInfo property (or populate it for me). This would probably require a callback for resolving the authorization header to authInfo.

Workaround

Currently, we must access authentication information through
this.props.authInfo in request handlers, which works but doesn't leverage the
transport's built-in authentication capabilities.

Activity

  1. added
    enhancement
    on Aug 27, 2025
  2. kentcdodds commented on Aug 28, 2025

    @kentcdodds
    Author

    To take this further, my current workaround is not recommended: https://discord.com/channels/595317990191398933/1354548448635912324/1410401842671779921

    Discord thread showing Kent asking how to add props to an McpAgent with a code snippet, and Kenton replying that ctx.props wasn’t intended for modification and may indicate a missing feature in the MCP framework.

    I think we need two things:

    1. An API to set this specifically.
    2. This to be set in the extra for request handlers.
  3. added
    mcp
    on Sep 16, 2025
  4. whoiskatrin commented on Sep 16, 2025

    @whoiskatrin
    Contributor

    Fixed by one of the resent PR's for now, we will consider more in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeenhancementmcp

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions