Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(mcp): answer 404 for a session id this stateless server never issued

nirholas1bef7d05292a
diff --git a/api/_mcp/auth.js b/api/_mcp/auth.js
index 9334dda7b..b9349a02b 100644
--- a/api/_mcp/auth.js
+++ b/api/_mcp/auth.js
@@ -204,8 +204,21 @@ export async function handleSse(req, res, { resourcePath = '/api/mcp', challenge
res.end();
}
-export function handleTerminate(_req, res) {
- // Stateless per-request server — nothing to tear down.
+export function handleTerminate(req, res) {
+ // This server is stateless per request and never issues an Mcp-Session-Id, so
+ // any session id a caller presents names a session that does not exist here.
+ // The Streamable HTTP transport says a server MUST answer 404 for a session
+ // id it no longer holds, which is the client's signal to start a fresh
+ // session; answering 204 would tell the client we tore down a session we
+ // never had. A DELETE with no session id is the ordinary polite teardown:
+ // nothing to release, 204.
+ const sessionId = req.headers?.['mcp-session-id'];
+ if (sessionId) {
+ res.statusCode = 404;
+ res.setHeader('content-type', 'application/json');
+ res.end(JSON.stringify({ error: 'unknown_session', message: 'no such MCP session; start a new one' }));
+ return;
+ }
res.statusCode = 204;
res.end();
}
diff --git a/api/openapi-json.js b/api/openapi-json.js
index ee3f32a57..172f3f9b9 100644
--- a/api/openapi-json.js
+++ b/api/openapi-json.js
@@ -275,16 +275,32 @@ export default wrap(async (req, res) => {
// Session teardown, part of the MCP Streamable HTTP transport and
// advertised in this route's own `allow` header. The server is
// stateless per request, so there is nothing to tear down and the
- // call always answers 204; a client that ends sessions politely
- // should still be able to find it in the spec.
+ // call answers 204; a client that ends sessions politely should
+ // still be able to find it in the spec. A caller that presents an
+ // Mcp-Session-Id names a session this server never issued, so that
+ // gets the transport's 404 "start a new session" instead.
delete: {
operationId: 'mcp_terminate_session',
security: [],
summary: 'Terminate an MCP session',
description:
- 'Ends the caller\'s MCP session. This server handles every request statelessly, so no session state is held and the call is a successful no-op.',
+ 'Ends the caller\'s MCP session. This server handles every request statelessly, so no session state is held and a plain DELETE is a successful no-op.',
+ parameters: [
+ {
+ name: 'Mcp-Session-Id',
+ in: 'header',
+ required: false,
+ schema: { type: 'string' },
+ description:
+ 'Session to end. This server never issues one, so any value names a session it does not hold.',
+ },
+ ],
responses: {
204: { description: 'Session ended; no content' },
+ 404: {
+ description:
+ 'Unknown session: the supplied Mcp-Session-Id was never issued by this server, so the client should start a new session',
+ },
},
},
},
diff --git a/tests/api/mcp.test.js b/tests/api/mcp.test.js
index 867ec627d..67736d6e9 100644
--- a/tests/api/mcp.test.js
+++ b/tests/api/mcp.test.js
@@ -441,6 +441,19 @@ describe('Protocol layer', () => {
expect(status).toBe(204);
});
+ it('DELETE with an Mcp-Session-Id returns 404 (no such session here)', async () => {
+ // This server never issues a session id, so any id a client presents came
+ // from somewhere else. The Streamable HTTP transport reads 404 as "start a
+ // new session"; 204 would claim we tore down a session we never held.
+ const { status, body } = await invoke({
+ method: 'DELETE',
+ headers: { 'mcp-session-id': 'sess-from-another-server' },
+ });
+
+ expect(status).toBe(404);
+ expect(body.error).toBe('unknown_session');
+ });
+
it('GET without bearer returns 401 + WWW-Authenticate (OAuth discovery) with x402 envelope attached', async () => {
// MCP protocol clients (mcp-protocol-version header) get 401 per the MCP
// authorization spec; bare x402 agents get 402 (covered in Authentication).