You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix MCP OAuth callback URL leaking instance name (#28183)
* Document callbackPath option for MCP OAuth callback security
Add documentation for the new callbackPath parameter in addMcpServer() that allows custom OAuth callback URL paths. This is required when sendIdentityOnConnect is false to prevent instance name leakage.
Changes:
- Add callbackPath parameter to addMcpServer() API reference
- Add security section explaining when and why callbackPath is required
- Include example showing how to route custom callback paths with getAgentByName
- Note that callback matching uses state parameter, not URL path
Relates to cloudflare/agents#868
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Fix export and await in code example
Co-authored-by: elithrar <elithrar@users.noreply.github.com>
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: elithrar <elithrar@users.noreply.github.com>
Copy file name to clipboardExpand all lines: src/content/docs/agents/model-context-protocol/mcp-client-api.mdx
+64-1Lines changed: 64 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,6 +190,67 @@ For example: `https://my-worker.workers.dev/agents/my-agent/default/callback`
190
190
191
191
OAuth tokens are securely stored in SQLite, and persist across agent restarts.
192
192
193
+
### Protecting instance names in OAuth callbacks
194
+
195
+
When using `sendIdentityOnConnect: false` to hide sensitive instance names (like session IDs or user IDs), the default OAuth callback URL would expose the instance name. To prevent this security issue, you must provide a custom `callbackPath`.
OAuth callbacks are matched by the `state` query parameter (format: `{serverId}:{stateValue}`), not by URL path. This means your custom `callbackPath` can be any path you choose, as long as requests to that path are routed to the correct agent instance.
251
+
252
+
:::
253
+
193
254
### Custom OAuth callback handling
194
255
195
256
Configure how OAuth completion is handled. By default, successful authentication redirects to your application origin, while failed authentication displays an HTML error page.
@@ -419,6 +480,7 @@ async addMcpServer(
419
480
url: string,
420
481
options?: {
421
482
callbackHost?: string;
483
+
callbackPath?:string;
422
484
agentsPrefix?:string;
423
485
client?:ClientOptions;
424
486
transport?: {
@@ -438,7 +500,8 @@ async addMcpServer(
438
500
-`url` (string, required) — URL of the MCP server endpoint
-`callbackHost` — Host for OAuth callback URL. If omitted, automatically derived from the incoming request
441
-
-`agentsPrefix` — URL prefix for OAuth callback path. Default: `"agents"`
503
+
-`callbackPath` — Custom callback URL path that bypasses the default `/agents/{class}/{name}/callback` construction. **Required when `sendIdentityOnConnect` is `false`** to prevent leaking the instance name. When set, the callback URL becomes `{callbackHost}/{callbackPath}`. You must route this path to the agent instance via `getAgentByName`
504
+
-`agentsPrefix` — URL prefix for OAuth callback path. Default: `"agents"`. Ignored when `callbackPath` is provided
442
505
-`client` — MCP client configuration options (passed to `@modelcontextprotocol/sdk` Client constructor). By default, includes `CfWorkerJsonSchemaValidator` for validating tool parameters against JSON schemas
443
506
-`transport` — Transport layer configuration:
444
507
-`headers` — Custom HTTP headers for authentication
0 commit comments