eca icon indicating copy to clipboard operation
eca copied to clipboard

Send the chat id as `_meta.sessionId` on MCP `tools/call`

Open fmjrey opened this issue 3 weeks ago • 2 comments

To upvote this issue, give it a thumbs up. See this list for the most upvoted issues.

Summary

ECA's MCP client sends no per-call identity to MCP servers. A server therefore cannot tell which chat a tool call belongs to, and any per-chat state it keeps is unreachable from the server side. Adding the chat id to the request _meta would fix this with a one-line change.

Current behaviour

src/eca/features/tools/mcp.clj builds every tools/call request and sets exactly one _meta field:

;; mcp.clj:1063-1065
;; plumcp drops a caller-supplied _meta, so the token is set directly.
request (-> (peg/make-call-tool-request name arguments)
            (assoc-in [:params :_meta :progressToken] progress-token))

There is no _meta.sessionId, and no equivalent is passed any other way.

The chatId (a client-generated UUID) exists in ECA's own protocol between the editor and the ECA server process, and is exposed as the ECA_CHAT_ID environment variable for locally spawned commands (src/eca/features/tools/shell.clj), but only when the caller supplies :chat-id, which the hook runner does not (src/eca/features/hooks.clj). An MCP server process is spawned once per workspace, so a per-chat environment variable could not work even if one were set.

Why it matters

A server that keeps per-chat state (session memory, journals, caches) must be able to address the current chat.

For example, the context-modeplugin's hooks journal rows under the ECA chat_id, while its MCP server falls back to a process-local id (mcp:<serverId>) because it cannot learn the chat id. The result is that ctx_search(scope:"session") — the documented way to ask "what did we do earlier in this session?" — returns nothing, even though the rows exist. Only scope: "worktree" / "project" work.

This is not specific to context-mode: any MCP server with per-session state hits the same wall.

What about other MCP clients?

This is a known gap in the broader MCP ecosystem. Several other clients do send a session/conversation ID in params._meta (e.g. OpenCode sends ai.opencode/sessionID, Codex sends thread_id, ZCode sends session_id), but ECA does not. The 2026-07-28 MCP spec is stateless (no Mcp-Session-Id header, no initialize handshake), so there is no protocol-level mechanism for this.

Proposed change

Send the chat id (and, if cheap, the current turn ~~as requested by #618~~) on each call:

request (-> (peg/make-call-tool-request name arguments)
            (assoc-in [:params :_meta :progressToken] progress-token)
            (assoc-in [:params :_meta :sessionId] chat-id))

The MCP spec allows arbitrary keys under _meta on a request, and the existing assoc-in already proves that a directly-set _meta field survives PluMCP serialization. call-tool! and request-tool-call in mcp.clj would need the chat id threaded through; the call sites already have it in scope.

Alternatives discarded

  • Environment variable per chat — not viable: the server process outlives a single chat.
  • A tool argument — pollutes every tool's schema and every server would have to opt in; _meta is the designated channel for this.
  • Re-spawn the server per chat — wasteful, and loses server state.

Acceptance criteria

  • A tools/call request carries params._meta.sessionId equal to the current chat id.
  • A server that reads params._meta.sessionId sees the same id ECA's hooks receive as chat_id in their stdin payload.
  • Servers that ignore _meta are unaffected.

Notes

  • Discovered while integrating context-mode v2 with ECA (see plugins/context-mode/hooks/ctx-eca-adapter.mjs from ECA plugins PR #14). Everything else in that integration works without core changes.
  • _meta.turn would additionally let a server correlate a call with the turn boundary, ~~which is requested by #618~~, but not required by context-mode.

fmjrey avatar Nov 09 '26 13:11 fmjrey

I don't like much these adhoc changes not following the protocol, but since each client has a way to do that we should have at least a way too, but let's keep an eye if this becomes some kind of standard, I will impl this meanwhile

ericdallo avatar Nov 09 '26 13:11 ericdallo

Thanks for taking this on. I just realized #618 is not about the MCP client, so I'll strike that out of the description.

fmjrey avatar Nov 09 '26 14:11 fmjrey