Skip to content

feat(mcp demos): move every McpAgent demo to createMcpHandler - #680

Merged
mattzcarey merged 2 commits into
mainfrom
mcp-demos-create-mcp-handler
Oct 22, 2026
Merged

mattzcarey merged 2 commits into
mainfrom
mcp-demos-create-mcp-handler

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Every MCP demo in this repo except the Descope one now runs a stateless createMcpHandler server on MCP SDK v2 instead of McpAgent. McpAgent has been deprecated since agents@0.20.0, and these demos are the starters that create-cloudflare and the Agents docs point people at.

Each demo now:

  • builds its McpServer in a createServer() factory with registerTool / registerResource, served by createMcpHandler from agents/mcp/server
  • reads OAuth props with getMcpAuthContext() (inside the factory, so tools can still depend on who is signed in) and bindings with env from cloudflare:workers
  • drops the MCP_OBJECT Durable Object binding and migration from wrangler.jsonc and worker-configuration.d.ts
  • depends on agents@^0.29.0 and @modelcontextprotocol/server@2.3.1 instead of @modelcontextprotocol/sdk
function createServer() {
	const server = new McpServer({ name: "Demo", version: "1.0.0" });
	const props = getMcpAuthContext()?.props as Props | undefined;
	if (props && ALLOWED_USERNAMES.has(props.login)) {
		server.registerTool("generateImage", { /* ... */ }, async (args) => {
			/* env.AI.run(...) */
		});
	}
	return server;
}

const mcpHandler = createMcpHandler(createServer);
export default new OAuthProvider({ apiRoute: "/mcp", apiHandler: { fetch: (req, env, ctx) => mcpHandler(req, env, ctx) }, /* ... */ });

The OAuth demos also move to @cloudflare/workers-oauth-provider@^1.2.4, still using the single-Worker OAuthProvider. 1.x requires resourceMetadata.resource, the URL every token is bound to, so each demo reads it from an MCP_RESOURCE var (as cloudflare-mcp does). The default is the local dev URL, and each README says to set it to the deployed URL. The removed allowPlainPKCE option is dropped.

Demos that verify the caller themselves (mcp-server-bearer-auth, remote-mcp-cf-access-self-hosted) pass the identity with createMcpHandler(createServer, { authContext: { props } }). The Stytch demos keep their Hono middleware, which sets ctx.props. READMEs now say /mcp and Streamable HTTP instead of /sse; none of these servers served SSE.

The first commit is @harshil1712's #633 (remote-mcp-cf-access, remote-mcp-github-oauth), cherry-picked with authorship, so this supersedes it. remote-mcp-server-descope-auth is left to #646 / #669.

  • tsc --noEmit and wrangler deploy --dry-run pass for every changed demo, and vite build for the two Stytch demos.
  • Ran remote-mcp-server under wrangler dev through the full OAuth flow (registration, /authorize, token, authorized tools/call) on 1.2.4.
  • Ran mcp-server-bearer-auth under wrangler dev: /mcp returns 401 without a token, and getToken returns the bearer token through getMcpAuthContext().

harshil1712 and others added 2 commits October 21, 2026 19:21
Rewrite the remaining McpAgent demos as stateless createMcpHandler servers on
MCP SDK v2 (@modelcontextprotocol/server 2.3.1, agents ^0.29.0). Props come
from getMcpAuthContext(), bindings from cloudflare:workers, and the MCP Durable
Object bindings and migrations are removed. READMEs point at /mcp and
Streamable HTTP instead of /sse.
@changeset-bot

changeset-bot Bot commented Oct 21, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e1ff49d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

npx https://pkg.pr.new/cloudflare/ai/ai-gateway-provider@680
npx https://pkg.pr.new/cloudflare/ai/ai-search-provider@680
npx https://pkg.pr.new/cloudflare/ai/@cloudflare/tanstack-ai@680
npx https://pkg.pr.new/cloudflare/ai/workers-ai-provider@680

commit: e1ff49d

@mattzcarey
mattzcarey merged commit e640093 into main Oct 22, 2026
4 checks passed
@mattzcarey
mattzcarey deleted the mcp-demos-create-mcp-handler branch October 22, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants