Skip to content

Make McpAgent extend Agent + Streaming HTTP protocol features#415

Merged
deathbyknowledge merged 22 commits into
cloudflare:mainfrom
deathbyknowledge:mcp-agent-extend-base-agent
Sep 11, 2025
Merged

Make McpAgent extend Agent + Streaming HTTP protocol features#415
deathbyknowledge merged 22 commits into
cloudflare:mainfrom
deathbyknowledge:mcp-agent-extend-base-agent

Conversation

@deathbyknowledge
Copy link
Copy Markdown
Contributor

@deathbyknowledge deathbyknowledge commented Aug 18, 2025

Lib changes:

Tests:

  • New tests to check the updated Streamable HTTP bits
  • Added E2E tests that deploy (using alchemy) complete MCP servers and test expected behavior with @modelcontextprotocol/sdk. They include authless and OAuth MCP examples.

Streamable HTTP changes adapted from the TS SDK

Run the E2E from /packages/agents with

npm run test:e2e

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Aug 18, 2025

🦋 Changeset detected

Latest commit: 4e1794f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
agents Patch
hono-agents Patch

Not sure what this means? Click here to learn what changesets are.

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

Comment thread packages/agents/src/mcp/index.ts Outdated
* @returns An error response object with status code and message
*/
onError(error: Error): { status: number; message: string } {
onMcpError(error: Error): { status: number; message: string } {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a breaking change. The old McpAgent had a custom onError method that clashes with the base Agent method with the same name once we extend it.

@deathbyknowledge deathbyknowledge marked this pull request as ready for review August 19, 2025 07:40
Comment thread packages/agents/src/mcp/index.ts Outdated
Comment thread packages/agents/src/mcp/index.ts Outdated
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Aug 19, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@415

commit: 4e1794f

@whoiskatrin
Copy link
Copy Markdown
Contributor

this is some really good stuff! @deathbyknowledge great work

Copy link
Copy Markdown
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tentative approval pending nits and notes

Comment thread .changeset/perfect-feet-breathe.md Outdated
Comment thread examples/mcp/src/server.ts Outdated
Comment thread packages/agents/src/index.ts Outdated
Comment thread packages/agents/src/mcp/index.ts Outdated
onmessage?: (message: JSONRPCMessage) => void;
sessionId?: string;
async setInitialized() {
await this.ctx.storage.put("initialized", true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await this.ctx.storage.put("initialized", true);
// todo: move to sync api once https://github.com/cloudflare/workerd/pull/4895 lands
await this.ctx.storage.put("initialized", true);

Comment thread packages/agents/src/mcp/transport.ts
Comment thread packages/agents/src/mcp/utils.ts
Comment thread packages/agents/src/mcp/index.ts Outdated
Comment thread packages/agents/src/mcp/utils.ts Outdated
Comment thread packages/agents/src/e2e/e2e.test.ts Outdated
Comment thread packages/agents/src/e2e/remote-mcp-authless/index.ts
Comment thread packages/agents/src/e2e/remote-mcp-server/index.ts
Comment thread packages/agents/src/e2e/remote-mcp-server/index.ts
@deathbyknowledge deathbyknowledge force-pushed the mcp-agent-extend-base-agent branch from c26e919 to 969371c Compare September 3, 2025 13:51
@kentcdodds
Copy link
Copy Markdown

How confident are we that this is going to be merged? Can I safely prepare teaching material based on this PR?

@deathbyknowledge
Copy link
Copy Markdown
Contributor Author

How confident are we that this is going to be merged? Can I safely prepare teaching material based on this PR?

We're waiting to release the AI SDK v5 changes to merge this. Should land this week

@kentcdodds
Copy link
Copy Markdown

That is helpful context. Do you have an idea/recommendation for getting:

  1. Authentication information into tool handlers
  2. request.url information into tool handlers

I know that the extra arg is a TypeScript MCP SDK-specific thing, but this implementation uses the TypeScript MCP SDK so it seems reasonable to expect to be able to use that. But at this point I don't care all that much, I just would like there to be some recommended way to accomplish this. Everything I've tried I've been told that it's not the right way to do it 🙃

@deathbyknowledge
Copy link
Copy Markdown
Contributor Author

That is helpful context. Do you have an idea/recommendation for getting:

The comment I left here #426 (comment) should cover all of those

@kentcdodds
Copy link
Copy Markdown

Just saw your message on my PR: #426 (comment)

Thanks for that!

So I take it from your message that it is fine/encouraged/recommended for me to modify ctx.props?

@deathbyknowledge
Copy link
Copy Markdown
Contributor Author

It is fine in the context of McpAgent as it's the only to customize the props as of now, as long as it happens in the handlers mentioned in the comment. We'll be improving the ergonomics regardless, to avoid going through so many hoops and possible footguns. I'll make sure to add some docs to cover this in detail.

@kentcdodds
Copy link
Copy Markdown

Sounds good. Thank you for your help!

@kentcdodds
Copy link
Copy Markdown

Oh, one question to be clear. How does McpAgent plan to support the things the extra argument offers:

Autocomplete dropdown in a code editor showing available properties: _meta?, authInfo?, requestId, requestInfo?, sendNotification, sendRequest, sessionId?, and signal.

@deathbyknowledge
Copy link
Copy Markdown
Contributor Author

That still needs to be thought out, it might not be directly though an extra argument but not sure yet

@kentcdodds
Copy link
Copy Markdown

That's fine. Just my 2¢, I think it would be good for the ecosystem if Cloudflare's implementation either supported the full TypeScript SDK (preferred) or built its own. Supporting part of the SDK will lead to confusion for folks I think.

@deathbyknowledge deathbyknowledge merged commit f7bd395 into cloudflare:main Sep 11, 2025
3 checks passed
@threepointone threepointone mentioned this pull request Sep 11, 2025
@remorses
Copy link
Copy Markdown

LFG

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.

5 participants