Skip to content

[FEATURE]: Expose session context to MCP servers via environment variables #15117

@adiled

Description

@adiled

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Related

Describe the enhancement you want to request

Problem

MCP servers spawned by OpenCode have no way to identify which session invoked a tool call. While #9292 and PR #9289 address this for the bash tool and prompt shell, MCP servers are spawned via StdioClientTransport in mcp/index.ts and do not receive session context.

This creates a gap for multi-session orchestration systems where:

  • Multiple OpenCode sessions run concurrently
  • A shared MCP server handles tool calls from all sessions
  • The MCP server needs to route responses or state back to the correct session

Without session identification, the MCP server cannot correlate tool calls to their originating session.

Use Case

An orchestration system manages multiple concurrent OpenCode sessions, each working on a different task. When a session calls an MCP tool (e.g., send_notification or log_progress), the MCP server needs to know which session made the call to route the output to the correct destination.

Currently, the only workaround is requiring the LLM to pass an identifier as a tool parameter, which is error-prone and adds cognitive load to the agent.

Proposed Solution

Expose session context to MCP servers, similar to PR #9289:

  • OPENCODE_SESSION_ID: The unique session identifier (e.g., ses_xyz123)
  • OPENCODE_SESSION_TITLE: The human-readable session title

Additional context

This would enable building robust multi-session orchestration systems on top of OpenCode where MCP servers can maintain session affinity for routing, logging, and state management.

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions