Feature: Pass session identifier to MCP servers #3926

Open
opened 2026-02-16 17:41:58 -05:00 by yindo · 2 comments
Owner

Originally created by @hosseinalipour on GitHub (Dec 28, 2025).

Originally assigned to: @thdxr on GitHub.

Summary

OpenCode currently does not pass any session identifier to MCP servers during tool calls, preventing MCP servers from maintaining session context or tracking interactions across multiple calls within a single OpenCode session.

Current Behavior

  • Session IDs are generated using ses_ prefix (see /packages/opencode/src/id/id.ts)
  • Sessions are stored at storage/session/{projectID}/{sessionID}.json
  • MCP tool calls in /packages/opencode/src/mcp/index.ts only pass name and arguments to client.callTool()
  • No session context is available to MCP servers

Desired Behavior

MCP servers should receive the OpenCode session identifier, enabling:

  • Session tracking and analytics
  • Contextual awareness across multiple tool calls
  • Better debugging and monitoring
  • Support for session-specific state management in MCP servers

Proposed Implementation

Options to consider:

  1. Pass session ID as a custom header for remote MCP servers
  2. Include session ID in the request metadata for stdio MCP servers
  3. Add a configuration option to enable/disable this feature

Related Files

  • Session ID generation: packages/opencode/src/id/id.ts
  • MCP client & tool calls: packages/opencode/src/mcp/index.ts
  • Tool context: packages/opencode/src/tool/tool.ts

Question

Would you accept a contribution for this feature? I'm interested in implementing session identifier support for MCP tool calls.

Originally created by @hosseinalipour on GitHub (Dec 28, 2025). Originally assigned to: @thdxr on GitHub. ## Summary OpenCode currently does not pass any session identifier to MCP servers during tool calls, preventing MCP servers from maintaining session context or tracking interactions across multiple calls within a single OpenCode session. ## Current Behavior - Session IDs are generated using `ses_` prefix (see `/packages/opencode/src/id/id.ts`) - Sessions are stored at `storage/session/{projectID}/{sessionID}.json` - MCP tool calls in `/packages/opencode/src/mcp/index.ts` only pass `name` and `arguments` to `client.callTool()` - No session context is available to MCP servers ## Desired Behavior MCP servers should receive the OpenCode session identifier, enabling: - Session tracking and analytics - Contextual awareness across multiple tool calls - Better debugging and monitoring - Support for session-specific state management in MCP servers ## Proposed Implementation Options to consider: 1. Pass session ID as a custom header for remote MCP servers 2. Include session ID in the request metadata for stdio MCP servers 3. Add a configuration option to enable/disable this feature ## Related Files - Session ID generation: `packages/opencode/src/id/id.ts` - MCP client & tool calls: `packages/opencode/src/mcp/index.ts` - Tool context: `packages/opencode/src/tool/tool.ts` ## Question Would you accept a contribution for this feature? I'm interested in implementing session identifier support for MCP tool calls.
Author
Owner

@github-actions[bot] commented on GitHub (Dec 28, 2025):

This feature is related to #6142, which addresses adding sessionID to plugin hooks for tracing integration. Both requests aim to pass session context to external systems (MCP servers and plugins respectively) to enable better session tracking and observability.

Consider if the implementation approach in #6142 could inform the design for passing session identifiers to MCP servers.

@github-actions[bot] commented on GitHub (Dec 28, 2025): This feature is related to #6142, which addresses adding sessionID to plugin hooks for tracing integration. Both requests aim to pass session context to external systems (MCP servers and plugins respectively) to enable better session tracking and observability. Consider if the implementation approach in #6142 could inform the design for passing session identifiers to MCP servers.
Author
Owner

@hosseinalipour commented on GitHub (Dec 29, 2025):

The linked issue (#6142) is about passing sessionId to internal plugin hooks, which is a different mechanism than MCP servers.

I researched the MCP protocol and found there's no standard convention for passing clientId/sessionId to MCP servers. For stdio transport, environment variables seem like the cleanest approach since OpenCode spawns the MCP process.

I can submit a PR that passes sessionId and projectId via environment variables (e.g., OPENCODE_SESSION_ID, OPENCODE_PROJECT_ID) when launching MCP servers. Would that approach work?

@hosseinalipour commented on GitHub (Dec 29, 2025): The linked issue (#6142) is about passing sessionId to internal plugin hooks, which is a different mechanism than MCP servers. I researched the MCP protocol and found there's no standard convention for passing clientId/sessionId to MCP servers. For stdio transport, environment variables seem like the cleanest approach since OpenCode spawns the MCP process. I can submit a PR that passes sessionId and projectId via environment variables (e.g., `OPENCODE_SESSION_ID`, `OPENCODE_PROJECT_ID`) when launching MCP servers. Would that approach work?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#3926