> ## Documentation Index
> Fetch the complete documentation index at: https://docs.embededitor.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Your Own MCP Relay

> Run the MCP relay on your own server, at your own address, step by step, with the @embededitor/relay package.

You don't need this page to use MCP: every editor links through ours, at `https://agent.embededitor.com`, with your license's public key. Run your own relay when:

* the link and **How to Connect** must show your own address, not ours (a white-label Full);
* only your signed-in users may make a link;
* the agent's calls must pass only through your own servers.

The relay is a small server between the editor's tab and the user's AI agent. The editor connects to it, the agent calls it over MCP, and it passes each call to the editor and the answer back. It keeps no photo and no call. It's a public package on npm, [`@embededitor/relay`](https://www.npmjs.com/package/@embededitor/relay), under the MIT license, and it runs on Node or on [Cloudflare Workers](#on-cloudflare-workers), with the same settings. Embed Editor's own relay is this package on Cloudflare.

The steps below run it on Node. To start from a ready project instead, see [Deploy a Template](#deploy-a-template).

## Set It Up

The examples use a shop, Acme, whose editor runs at `https://shop.example.com` and whose relay will run at `https://agent.shop.example.com`.

**1. Install**

In the project of your server, on Node 20 or later:

```bash npm theme={"dark"}
npm install @embededitor/relay
```

```bash yarn theme={"dark"}
yarn add @embededitor/relay
```

```bash pnpm theme={"dark"}
pnpm add @embededitor/relay
```

```bash bun theme={"dark"}
bun add @embededitor/relay
```

**2. Create the Relay**

Create the relay with its three settings, and start it. They're plain values: write them in the code, or read them from wherever you keep your configuration.

```js relay.js theme={"dark"}
import { createRelay } from '@embededitor/relay';

const relay = createRelay({
  agentSessionSecret: null,
  allowedOrigins: ['https://shop.example.com'],
  publicOrigin: 'https://agent.shop.example.com'
});

relay.listen(8788);
```

| Setting | What it does |
| - | - |
| `allowedOrigins` | The pages whose editor may make a link: `['https://shop.example.com']`. See [Which Pages Make a Link](#which-pages-make-a-link). |
| `agentSessionSecret` | A secret your backend uses to make links for signed-in users only. `null` if the editor makes its own links. See [Only Signed-In Users](#only-signed-in-users). |
| `publicOrigin` | The address the links name, the one your users' browsers and agents reach. `null` uses the address each request came in on. |

With no page in `allowedOrigins` and no `agentSessionSecret`, the relay makes no link.

To serve it from a server of your own instead, see [In Your Own Server](#in-your-own-server).

**3. Put It Online**

```bash theme={"dark"}
node relay.js
```

The relay listens on port 8788. Put it at a public HTTPS address of its own, `https://agent.shop.example.com`, the way you serve your other apps: a reverse proxy, a load balancer or your hosting platform. It needs WebSockets, which most do.

Claude, ChatGPT, Grok and Perplexity reach the relay from their own servers, so it must be public, at an `https://` address. Keep it on the standard port, 443: Claude can't reach an address with another port. A relay on `localhost` works only for agents on the same computer, like Claude Code, Codex and Cursor.

**4. Point the Editor at It**

Give the editor your relay's address in `agentRelay`, with `enableAgent`. Your own relay needs no public key:

```tsx React theme={"dark"}
import { EmbedEditor } from '@embededitor/full/react';

const PhotoEditor = ({ src }: { src: string }) => (
  <div style={{ height: 600 }}>
    <EmbedEditor agentRelay='https://agent.shop.example.com' enableAgent src={src} />
  </div>
);

export default PhotoEditor;
```

```js JavaScript theme={"dark"}
import { mountEditor } from '@embededitor/full/standalone';

mountEditor(document.getElementById('editor'), {
  agentRelay: 'https://agent.shop.example.com',
  enableAgent: true,
  src: '/photos/sunset.jpg'
});
```

**5. Try It**

1. Connect your agent to your relay's MCP address, `https://agent.shop.example.com/mcp`. In Claude Code:

   ```bash theme={"dark"}
   claude mcp add --transport http photo-editor https://agent.shop.example.com/mcp
   ```

   **How to Connect**, in the editor's card, shows your users this address and the steps for each agent ([Connecting Their Agent](https://docs.embededitor.com/mcp/overview.md#connecting-their-agent)).

2. Tap **Agent** in the editor, copy the link, and paste it into your agent with a request.

## Which Pages Make a Link

`allowedOrigins` lists the pages whose editor makes its own link, straight from the browser. Each entry is one of:

| Entry | Matches |
| - | - |
| `'https://shop.example.com'` | That page's origin alone: not `https://www.shop.example.com`, not `http://shop.example.com`. |
| `'https://*.shop.example.com'` | Every subdomain, like `https://eu.shop.example.com`, but not `https://shop.example.com` itself: list it too if its pages make links. |
| `'*'` | Every page, on any site. The relay counts no requests: with `'*'`, put a check of your own in front of `POST /sessions`, or a rate limit at your proxy or host. |

An origin is the scheme, the host and the port, as the address bar shows them, with no path: `https://shop.example.com`, `http://localhost:5173`.

This stops other sites' pages, since the browser says which page is asking. A script outside a browser can claim any page, so where only your own users may make links, use your backend.

For a check of your own in front of `POST /sessions`, `@embededitor/relay` exports `relay.access(request)` (whether the relay makes a link for it, with the CORS headers its answer carries), `originAllowed({ entries, origin })` (whether one of `entries`, read as `allowedOrigins` reads its own, names `origin`) and `readJson({ max, req })`: a request's body as JSON, read up to `max` bytes, `null` past them, for no body, for a body whose stream fails midway (an upload cut), and for one that isn't JSON. Read the body from `request.clone()`, so the relay gets the request whole.

## Only Signed-In Users

To let only signed-in users make a link, your backend makes it, with a secret only it and the relay know:

1. **Make a secret**, and keep it in your backend's secret store:

   ```bash theme={"dark"}
   openssl rand -hex 32
   ```

2. **Give it to the relay**, as `agentSessionSecret`, read from the same store:

   ```js relay.js theme={"dark"}
   import { createRelay } from '@embededitor/relay';

   import secrets from './secrets';

   const relay = createRelay({
     agentSessionSecret: await secrets.get('agent-session-secret'),
     allowedOrigins: [],
     publicOrigin: 'https://agent.shop.example.com'
   });

   relay.listen(8788);
   ```

   With `allowedOrigins` empty, no page makes a link on its own: only your backend does.

3. **Add a route to your backend** that checks the user's sign-in and asks the relay for a link. In [Hono](https://hono.dev):

   ```ts Your route theme={"dark"}
   import { Hono } from 'hono';

   // your own functions: the signed-in user from the request, and your secret store
   import { getSignedInUser } from './auth';
   import secrets from './secrets';

   const RELAY = 'https://agent.shop.example.com';

   const app = new Hono();

   app.post('/agent-link', async c => {
     const user = await getSignedInUser(c);

     if (!user) {
       return c.json({ error: 'UNAUTHORIZED' }, 401);
     }

     const secret = await secrets.get('agent-session-secret');
     const res = await fetch(`${RELAY}/sessions`, {
       headers: { authorization: `Bearer ${secret}` },
       method: 'POST'
     });

     if (!res.ok) {
       return new Response(null, { status: res.status });
     }

     const link = await res.json();

     return c.json(link);
   });

   export default app;
   ```

4. **Give the editor `onAgentLink`**, which calls your route when the user taps **Agent**:

   ```tsx React theme={"dark"}
   import { EmbedEditor } from '@embededitor/full/react';

   const createAgentLink = async () => {
     const res = await fetch('/agent-link', { credentials: 'include', method: 'POST' });

     if (!res.ok) {
       throw new Error(`${res.status}`);
     }

     return res.json();
   };

   const PhotoEditor = ({ src }: { src: string }) => (
     <div style={{ height: 600 }}>
       <EmbedEditor agentRelay='https://agent.shop.example.com' enableAgent onAgentLink={createAgentLink} src={src} />
     </div>
   );

   export default PhotoEditor;
   ```

   ```js JavaScript theme={"dark"}
   import { mountEditor } from '@embededitor/full/standalone';

   mountEditor(document.getElementById('editor'), {
     agentRelay: 'https://agent.shop.example.com',
     enableAgent: true,
     onAgentLink: async () => {
       const res = await fetch('/agent-link', { credentials: 'include', method: 'POST' });

       if (!res.ok) {
         throw new Error(`${res.status}`);
       }

       return res.json();
     },
     src: '/photos/sunset.jpg'
   });
   ```

The secret never reaches a page. Call the relay on its public address from your route, since the link names the address it was asked on. Make the link when the user taps, never ahead of time: a link whose editor doesn't connect within 2 minutes ends.

## In Your Own Server

`createRelay` also gives the relay's [Hono](https://hono.dev) app and the editor's socket server, to serve them from a Node server of yours:

```js theme={"dark"}
import { createRelay } from '@embededitor/relay';
import { serve } from '@hono/node-server';

const { app, websocket } = createRelay({
  agentSessionSecret: null,
  allowedOrigins: ['https://shop.example.com'],
  publicOrigin: 'https://agent.shop.example.com'
});

serve({ fetch: app.fetch, port: 8788, websocket });
```

The relay answers at the root of its address, so give it a host of its own. Every link lives in the process's memory: run one process per relay.

## On Cloudflare Workers

The same relay runs as a Cloudflare Worker, from `@embededitor/relay/cloudflare`. Each link is a Durable Object, so the relay runs on any number of Cloudflare's machines, with nothing else to set up.

**1. Install**

In your Worker's project:

```bash npm theme={"dark"}
npm install @embededitor/relay
```

```bash yarn theme={"dark"}
yarn add @embededitor/relay
```

```bash pnpm theme={"dark"}
pnpm add @embededitor/relay
```

```bash bun theme={"dark"}
bun add @embededitor/relay
```

**2. Create the Relay**

The Worker is the relay's `fetch` and the Durable Object class it exports, `EditorSession`. Its settings are the same three as on Node:

```ts src/index.ts theme={"dark"}
import { createRelay, EditorSession } from '@embededitor/relay/cloudflare';

const relay = createRelay({
  agentSessionSecret: null,
  allowedOrigins: ['https://shop.example.com'],
  publicOrigin: null
});

export { EditorSession };
export default { fetch: relay.fetch };
```

**3. Bind Its Durable Object**

The relay reads its Durable Object under the binding `EDITOR_SESSION`:

```jsonc wrangler.jsonc theme={"dark"}
{
  "compatibility_date": "2026-10-12",
  "durable_objects": {
    "bindings": [{ "class_name": "EditorSession", "name": "EDITOR_SESSION" }]
  },
  "main": "src/index.ts",
  "migrations": [{ "new_sqlite_classes": ["EditorSession"], "tag": "v1" }],
  "name": "shop-agent-relay"
}
```

Keep the class name and the migration once deployed: the Worker's Durable Objects are found by them.

**4. Put It Online**

```bash theme={"dark"}
npx wrangler deploy
```

Then give the Worker a Custom Domain of its own, `agent.shop.example.com`, in its settings. The links name the address each request came in on, so `publicOrigin` stays `null`.

Point the editor at it and try it as on Node: [Point the Editor at It](#set-it-up).

## Deploy a Template

Two ready projects come in the package, each a folder to copy whole, set and deploy (`node_modules/@embededitor/relay/templates/` once it's installed):

| Folder | What it is |
| - | - |
| `templates/cloudflare` | A Worker: `src/index.ts` with the settings in code, and `wrangler.jsonc` with its Durable Object and migration. `npm run deploy` puts it on Cloudflare. |
| `templates/node` | A Node server: `index.js` with the settings in code, listening on the port its host gives, and `render.yaml` (at the public repository's root, for Render's button; not in the package). Railway runs it as a plain Node app. |

In each, set `allowedOrigins` to your editor's pages before you deploy. Its README says the rest.

To deploy without copying the folder, the buttons below start from the templates in the public repository, [`embededitor/relay`](https://github.com/embededitor/relay), a copy of the package's source:

[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/embededitor/relay/tree/main/templates/cloudflare)

[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/embededitor/relay)

Cloudflare's button provisions the Worker and its Durable Object from `wrangler.jsonc`, then asks for the settings in `src/index.ts`. Render's button reads `render.yaml` at the repository's root, which points at `templates/node`. On Railway, make a new project from the GitHub repository `embededitor/relay` and set its root directory to `templates/node`: Railway has no button for it, since that needs a template published on Railway's dashboard.

## Troubleshooting

### The card says "Couldn’t Create the Link"

The relay made no link. The browser's console says why, after `embed-editor: the agent link wasn't made.`

* Without `onAgentLink`: your page's origin isn't in `allowedOrigins`, and the browser's console shows a CORS error on `/sessions`. Add the origin as the address bar shows it, like `https://shop.example.com` or `http://localhost:5173`, and restart the relay.
* With `onAgentLink`: your route didn't answer with a link. See the next one.

### Your route gets 403 from the relay

The secret your route sends isn't the relay's `agentSessionSecret`, or the relay has none. Give both the same value, from the same store.

### The link shows the wrong address

The relay names the address it was asked on. Set `publicOrigin` to its public address, or call it there.

### Claude or ChatGPT can't reach the relay

They call it from their own servers, so it needs a public HTTPS address. `localhost` works only for agents on the same computer.

## Limits

| Limit | Value |
| - | - |
| A link's lifetime | 2 hours |
| A tab away before its link ends | 2 minutes |
| A call's wait for the editor | 55 s |
| An ended link's record, so a late call hears why | 1 hour |
| The editor's commands and its text for the agent, together | 1 MiB, counted as stored: two bytes a character (the Full's are about 82,000 characters) |
| A message from the editor (a whole-photo screenshot is about 113 KB) | 8 MiB on Node, 32 MiB on Cloudflare |

To build a relay in another language or stack, see [Protocol](https://docs.embededitor.com/mcp/protocol.md).

> [card] [MCP](https://docs.embededitor.com/mcp/overview.md)
>
> Add MCP to your editor, and what your users see.

> [card] [Protocol](https://docs.embededitor.com/mcp/protocol.md)
>
> Every route, message and timing, to build a relay in any stack.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.