Skip to main content

Overview

Every tool handler receives a ToolContext as its second argument. It carries the current session ID, resolved authentication headers, provider token, and an abort signal for cancellation.
When using createMCPServer<TEnv>, the context includes your typed environment bindings:

Properties

string
The current MCP session ID. Generated by the server on the initialize handshake. Use this to correlate tool calls to a specific client session in logs or external systems.
TEnv | undefined
Your Cloudflare environment bindings. Only available when using createMCPServer<TEnv>(). Use this to access KV namespaces, D1 databases, R2 buckets, and other Workers bindings with full type safety.
AbortSignal | undefined
An AbortSignal for cooperative cancellation. When the MCP client sends a notifications/cancelled message, the server aborts the signal. Pass this to fetch or other async operations so they stop early when the client cancels.
{ progressToken?: string | number; requestId?: string } | undefined
Request metadata forwarded from the MCP JSON-RPC layer.
  • progressToken — opaque token the client uses to track progress notifications.
  • requestId — the JSON-RPC request ID for this call.
'oauth' | 'bearer' | 'api_key' | 'custom' | 'none' | undefined
The active authentication strategy for this server instance.
string | undefined
The resolved access token for the authenticated user.
  • OAuth: the provider access token mapped from the RS token (e.g., a Google or GitHub token).
  • Bearer: the value of the BEARER_TOKEN environment variable.
  • API key: the value of the API_KEY environment variable.
  • Custom / none: undefined.
Use resolvedHeaders for making API calls instead of constructing the Authorization header yourself.
ProviderInfo | undefined
OAuth provider details. Only populated when authStrategy is oauth.
Record<string, string> | undefined
Ready-to-use HTTP headers for forwarding authentication to external APIs. The exact headers depend on the active strategy:Spread or pass this object directly to fetch:
Record<string, string> | undefined
deprecated
Raw authorization headers from the incoming MCP request (before resolution). Use resolvedHeaders instead — it contains the correct auth headers for forwarding to external services regardless of the active strategy.

Helper Methods

ToolContext includes helper methods that return a { data, error } pattern for safe error handling.

getToken()

Returns the access token or an error if not available.

getUser()

Fetches user info from the OAuth provider. Works with Google, GitHub, and other OAuth providers.
This is a convenience wrapper around getUser() + USERINFO_ENDPOINTS that handles the userinfo URL automatically based on the auth strategy.

AuthenticatedToolContext

AuthenticatedToolContext extends ToolContext and guarantees that providerToken is a non-optional string. The dispatcher populates this automatically for tools that declare requiresAuth: true.
You can also use assertProviderToken to narrow the type manually:

Examples

Forward auth to an external API

Use session ID for logging

Inspect auth strategy