You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
14
13
:::
15
14
16
15
Workers provide a built-in, JavaScript-native [RPC (Remote Procedure Call)](https://en.wikipedia.org/wiki/Remote_procedure_call) system, allowing you to:
17
16
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.
20
20
21
21
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.
22
22
@@ -42,11 +42,11 @@ As an exception to Structured Clone, application-defined classes (or objects wit
42
42
43
43
The RPC system also supports a number of types that are not Structured Cloneable, including:
44
44
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.
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
244
242
245
243
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.
246
244
247
-
<Streamid="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
+
/>
248
250
249
251
## More Details
250
252
251
253
<DirectoryListing />
252
254
253
255
## Limitations
254
256
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.
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.
0 commit comments