Skip to content

Commit 2fe8497

Browse files
committed
feat: example readme
1 parent dafa564 commit 2fe8497

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

examples/rpc-transport/README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
1-
# RPC Transport demo
1+
# RPC Transport for MCP
2+
3+
Example showing an `Agent` calling an `McpAgent` within the same worker using a custom RPC transport.
4+
5+
## Why RPC Transport?
6+
7+
If your MCP server and your agent/client are both deployed to the Cloudflare developer platform, our RPC transport is the fastest way to connect them:
8+
9+
- **No network overhead** - Direct function calls instead of HTTP
10+
- **Simpler** - No endpoints to configure, no connection management, no authentication.
11+
12+
## How it works
13+
14+
Both the agent (MCP client) and MCP server can exist in the same Worker.
15+
16+
The MCP server is just a regular `McpAgent`:
17+
18+
```typescript
19+
export class MyMCP extends McpAgent<Env, State, {}> {
20+
server = new McpServer({
21+
name: "Demo",
22+
version: "1.0.0"
23+
});
24+
25+
async init() {
26+
this.server.tool(
27+
"add",
28+
"Add to the counter, stored in the MCP",
29+
{ a: z.number() },
30+
async ({ a }) => {
31+
this.setState({ ...this.state, counter: this.state.counter + a });
32+
return {
33+
content: [
34+
{
35+
text: `Added ${a}, total is now ${this.state.counter}`,
36+
type: "text"
37+
}
38+
]
39+
};
40+
}
41+
);
42+
}
43+
}
44+
```
45+
46+
The agent calls out to the MCP server using Cloudflare's RPC bindings:
47+
48+
```typescript
49+
export class Chat extends AIChatAgent<Env> {
50+
async onStart(): Promise<void> {
51+
// Connect to MyMCP server via RPC
52+
await this.addRpcMcpServer("test-server", "MyMCP");
53+
}
54+
55+
async onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>) {
56+
// MCP tools are now available
57+
const allTools = this.mcp.getAITools();
58+
59+
const result = streamText({
60+
model,
61+
tools: allTools
62+
// ...
63+
});
64+
}
65+
}
66+
```
67+
68+
## Instructions
69+
70+
1. Copy `.dev.vars.example` to `.dev.vars` and add your OpenAI API key
71+
2. Run `npm install`
72+
3. Run `npm start`
73+
4. Open the UI in your browser
74+
75+
Try asking the AI to add numbers to the counter!
76+
77+
## More Info
78+
79+
Sevice bindings over RPC are commonly used in Workers to call out to other Cloudflare services. You can find out more [in the docs](https://developers.cloudflare.com/workers/runtime-apis/bindings/).
80+
81+
The Model Context Protocol supports [pluggable transports](https://modelcontextprotocol.io/specification/2025-06-18/basic/transports). The code for this custom RPC transport can be found [here](packages/agents/src/mcp/rpc.ts)

0 commit comments

Comments
 (0)