Skip to content

Commit 32a4fc5

Browse files
committed
Reference MCP tools in RPC docs
1 parent d4fa6b6 commit 32a4fc5

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

src/content/docs/workers/runtime-apis/rpc/index.mdx

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ title: Remote-procedure call (RPC)
44
head: []
55
description: The built-in, JavaScript-native RPC system built into Workers and
66
Durable Objects.
7-
87
---
98

10-
import { DirectoryListing, Render, Stream, WranglerConfig } from "~/components"
9+
import { DirectoryListing, Render, Stream, WranglerConfig } from "~/components";
1110

1211
:::note
1312
To use RPC, [define a compatibility date](/workers/configuration/compatibility-dates) of `2024-04-03` or higher, or include `rpc` in your [compatibility flags](/workers/configuration/compatibility-flags/#nodejs-compatibility-flag).
1413
:::
1514

1615
Workers provide a built-in, JavaScript-native [RPC (Remote Procedure Call)](https://en.wikipedia.org/wiki/Remote_procedure_call) system, allowing you to:
1716

18-
* Define public methods on your Worker that can be called by other Workers on the same Cloudflare account, via [Service Bindings](/workers/runtime-apis/bindings/service-bindings/rpc)
19-
* Define public methods on [Durable Objects](/durable-objects) that can be called by other workers on the same Cloudflare account that declare a binding to it.
17+
- Define public methods on your Worker that can be called by other Workers on the same Cloudflare account, via [Service Bindings](/workers/runtime-apis/bindings/service-bindings/rpc)
18+
- Define public methods on [Durable Objects](/durable-objects) that can be called by other workers on the same Cloudflare account that declare a binding to it.
19+
- Define public methods on [Model Context Protocol (MCP) Servers](/agents/model-context-protocol/mcp-server/) that are automatically translated to MCP [tools](/agents/model-context-protocol/mcp-server/tools/) that can be called by MCP clients.
2020

2121
The RPC system is designed to feel as similar as possible to calling a JavaScript function in the same Worker. In most cases, you should be able to write code in the same way you would if everything was in a single Worker.
2222

@@ -42,11 +42,11 @@ As an exception to Structured Clone, application-defined classes (or objects wit
4242

4343
The RPC system also supports a number of types that are not Structured Cloneable, including:
4444

45-
* Functions, which are replaced by stubs that call back to the sender.
46-
* Application-defined classes that extend `RpcTarget`, which are similarly replaced by stubs.
47-
* [ReadableStream](/workers/runtime-apis/streams/readablestream/) and [WriteableStream](/workers/runtime-apis/streams/writablestream/), with automatic streaming flow control.
48-
* [Request](/workers/runtime-apis/request/) and [Response](/workers/runtime-apis/response/), for conveniently representing HTTP messages.
49-
* RPC stubs themselves, even if the stub was received from a third Worker.
45+
- Functions, which are replaced by stubs that call back to the sender.
46+
- Application-defined classes that extend `RpcTarget`, which are similarly replaced by stubs.
47+
- [ReadableStream](/workers/runtime-apis/streams/readablestream/) and [WriteableStream](/workers/runtime-apis/streams/writablestream/), with automatic streaming flow control.
48+
- [Request](/workers/runtime-apis/request/) and [Response](/workers/runtime-apis/response/), for conveniently representing HTTP messages.
49+
- RPC stubs themselves, even if the stub was received from a third Worker.
5050

5151
## Functions
5252

@@ -81,35 +81,33 @@ main = "./src/counter.js"
8181
import { WorkerEntrypoint, RpcTarget } from "cloudflare:workers";
8282

8383
class Counter extends RpcTarget {
84-
#value = 0;
84+
#value = 0;
8585

86-
increment(amount) {
87-
this.#value += amount;
88-
return this.#value;
89-
}
86+
increment(amount) {
87+
this.#value += amount;
88+
return this.#value;
89+
}
9090

91-
get value() {
92-
return this.#value;
93-
}
91+
get value() {
92+
return this.#value;
93+
}
9494
}
9595

9696
export class CounterService extends WorkerEntrypoint {
97-
async newCounter() {
98-
return new Counter();
99-
}
97+
async newCounter() {
98+
return new Counter();
99+
}
100100
}
101101

102102
export default {
103-
fetch() {
104-
return new Response("ok")
105-
}
106-
}
103+
fetch() {
104+
return new Response("ok");
105+
},
106+
};
107107
```
108108

109109
The method `increment` can be called directly by the client, as can the public property `value`:
110110

111-
112-
113111
<WranglerConfig>
114112

115113
```toml
@@ -124,18 +122,18 @@ services = [
124122

125123
```js
126124
export default {
127-
async fetch(request, env) {
128-
using counter = await env.COUNTER_SERVICE.newCounter();
125+
async fetch(request, env) {
126+
using counter = await env.COUNTER_SERVICE.newCounter();
129127

130-
await counter.increment(2); // returns 2
131-
await counter.increment(1); // returns 3
132-
await counter.increment(-5); // returns -2
128+
await counter.increment(2); // returns 2
129+
await counter.increment(1); // returns 3
130+
await counter.increment(-5); // returns -2
133131

134-
const count = await counter.value; // returns -2
132+
const count = await counter.value; // returns -2
135133

136-
return new Response(count);
137-
}
138-
}
134+
return new Response(count);
135+
},
136+
};
139137
```
140138

141139
:::note
@@ -189,24 +187,24 @@ This works when calling properties of objects returned by RPC methods as well. F
189187
import { WorkerEntrypoint } from "cloudflare:workers";
190188

191189
export class MyService extends WorkerEntrypoint {
192-
async foo() {
193-
return {
194-
bar: {
195-
baz: () => "qux"
196-
}
197-
}
198-
}
190+
async foo() {
191+
return {
192+
bar: {
193+
baz: () => "qux",
194+
},
195+
};
196+
}
199197
}
200198
```
201199

202200
```js
203201
export default {
204-
async fetch(request, env) {
205-
using foo = env.MY_SERVICE.foo();
206-
let baz = await foo.bar.baz();
207-
return new Response(baz);
208-
}
209-
}
202+
async fetch(request, env) {
203+
using foo = env.MY_SERVICE.foo();
204+
let baz = await foo.bar.baz();
205+
return new Response(baz);
206+
},
207+
};
210208
```
211209

212210
If the initial RPC ends up throwing an exception, then any pipelined calls will also fail with the same exception
@@ -244,13 +242,17 @@ Currently, this proxying only lasts until the end of the Workers' execution cont
244242

245243
In this video, we explore how Cloudflare Workers support Remote Procedure Calls (RPC) to simplify communication between Workers. Learn how to implement RPC in your JavaScript applications and build serverless solutions with ease. Whether you're managing microservices or optimizing web architecture, this tutorial will show you how to quickly set up and use Cloudflare Workers for RPC calls. By the end of this video, you'll understand how to call functions between Workers, pass functions as arguments, and implement user authentication with Cloudflare Workers.
246244

247-
<Stream id="d506935b6767fd07626adbec46d41e6d" title="Introduction to Workers RPC" thumbnail="2.5s" />
245+
<Stream
246+
id="d506935b6767fd07626adbec46d41e6d"
247+
title="Introduction to Workers RPC"
248+
thumbnail="2.5s"
249+
/>
248250

249251
## More Details
250252

251253
<DirectoryListing />
252254

253255
## Limitations
254256

255-
* [Smart Placement](/workers/configuration/smart-placement/) is currently ignored when making RPC calls. If Smart Placement is enabled for Worker A, and Worker B declares a [Service Binding](/workers/runtime-apis/bindings) to it, when Worker B calls Worker A via RPC, Worker A will run locally, on the same machine.
256-
* The maximum serialized RPC limit is 1 MB. Consider using [`ReadableStream`](/workers/runtime-apis/streams/readablestream/) when returning more data.
257+
- [Smart Placement](/workers/configuration/smart-placement/) is currently ignored when making RPC calls. If Smart Placement is enabled for Worker A, and Worker B declares a [Service Binding](/workers/runtime-apis/bindings) to it, when Worker B calls Worker A via RPC, Worker A will run locally, on the same machine.
258+
- The maximum serialized RPC limit is 1 MB. Consider using [`ReadableStream`](/workers/runtime-apis/streams/readablestream/) when returning more data.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Model Context Protocol (MCP) Tools
4+
external_link: /agents/model-context-protocol/mcp-server/tools/
5+
sidebar:
6+
order: 6
7+
head: []
8+
description: Write TypeScript methods using the JavaScript-native remote procedure call (RPC) system, and automatically translate these methods to Model Context Protocol (MCP) tools that can be called by MCP clients and AI agents.
9+
---

0 commit comments

Comments
 (0)