Skip to content

Feature: Pass session identifier to MCP servers #6279

Closed as not planned
Closed as not planned

Description

@hosseinalipour

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.

Activity

  1. github-actions commented on Dec 28, 2025

    @github-actions
    Contributor

    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.

  2. hosseinalipour commented on Dec 29, 2025

    @hosseinalipour
    Author

    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?

  3. thdxr commented on Mar 25, 2026

    @thdxr
    Member

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions