Skip to content

Commit 6e404aa

Browse files
committed
feat: example readme
1 parent dafa564 commit 6e404aa

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

examples/rpc-transport/README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
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+
This is very useful for internal applications. You can define `tools`, `prompts` and `resources` in your MCP server, expose that publically to your users, and also power your own `Agent` from the same `McpAgent`.
13+
14+
## How it works
15+
16+
Both the agent (MCP client) and MCP server can exist in the same Worker.
17+
18+
The MCP server is just a regular `McpAgent`:
19+
20+
```typescript
21+
export class MyMCP extends McpAgent<Env, State, {}> {
22+
server = new McpServer({
23+
name: "Demo",
24+
version: "1.0.0"
25+
});
26+
27+
async init() {
28+
this.server.tool(
29+
"add",
30+
"Add to the counter, stored in the MCP",
31+
{ a: z.number() },
32+
async ({ a }) => {
33+
this.setState({ ...this.state, counter: this.state.counter + a });
34+
return {
35+
content: [
36+
{
37+
text: `Added ${a}, total is now ${this.state.counter}`,
38+
type: "text"
39+
}
40+
]
41+
};
42+
}
43+
);
44+
}
45+
}
46+
```
47+
48+
The agent calls out to the MCP server using Cloudflare's RPC bindings:
49+
50+
```typescript
51+
export class Chat extends AIChatAgent<Env> {
52+
async onStart(): Promise<void> {
53+
// Connect to MyMCP server via RPC
54+
await this.addRpcMcpServer("test-server", "MyMCP");
55+
}
56+
57+
async onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>) {
58+
// MCP tools are now available
59+
const allTools = this.mcp.getAITools();
60+
61+
const result = streamText({
62+
model,
63+
tools: allTools
64+
// ...
65+
});
66+
}
67+
}
68+
```
69+
70+
## Instructions
71+
72+
1. Copy `.dev.vars.example` to `.dev.vars` and add your OpenAI API key
73+
2. Run `npm install`
74+
3. Run `npm start`
75+
4. Open the UI in your browser
76+
77+
Try asking the AI to add numbers to the counter!
78+
79+
## More Info
80+
81+
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/).
82+
83+
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)