Skip to content

feat(mcp): forward session ID and plugin _meta to MCP tool calls#21624

Open
Sathvik-1007 wants to merge 1 commit intoanomalyco:devfrom
Sathvik-1007:feat/mcp-forward-session-meta
Open

feat(mcp): forward session ID and plugin _meta to MCP tool calls#21624
Sathvik-1007 wants to merge 1 commit intoanomalyco:devfrom
Sathvik-1007:feat/mcp-forward-session-meta

Conversation

@Sathvik-1007
Copy link
Copy Markdown

Issue for this PR

Closes #6279
Closes #17084

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

MCP servers currently have no way to know which opencode session called them. This adds two things:

  1. Built-in session context: opencode now always includes _meta: { sessionID } in every callTool() request. MCP servers receive the caller's session ID automatically via the standard MCP _meta field.

  2. Plugin-extensible _meta: the tool.execute.before hook output now includes an optional _meta field. Plugins can add custom metadata (correlation IDs, trace context, etc.) that gets forwarded to downstream MCP servers alongside the built-in sessionID.

When no plugin sets _meta, MCP servers still receive { sessionID }. When a plugin adds fields, they merge with the built-in ones.

Files changed (3):

  • packages/opencode/src/mcp/index.ts — read _meta from execute opts, forward to client.callTool()
  • packages/opencode/src/session/prompt.ts — seed _meta with { sessionID }, pass through hook and into execute
  • packages/plugin/src/index.ts — add _meta to tool.execute.before hook output type

How did you verify your code works?

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

The following comment was made by an LLM, it may be inaccurate:

I found a related PR that may be a duplicate or predecessor:

Related PR Found:

This PR appears to be addressing the same core feature of forwarding metadata (_meta) to MCP tool calls. PR #21624 (the current PR) seems to be an enhancement that expands on this by also including the session ID as built-in context alongside plugin-extensible metadata.

Why they're related:

You may want to check if PR #21539 is still open and whether #21624 supersedes it or if they should be combined.

@Sathvik-1007
Copy link
Copy Markdown
Author

Relationship to #21539

This PR supersedes #21539. Same 3 files, same approach. Diff comparison:

  • mcp/index.ts — identical changes (read _meta from opts, forward to callTool())
  • plugin/src/index.ts — identical (_meta added to hook output type)
  • prompt.ts — only difference: feat(mcp): forward plugin-set _meta to downstream MCP tool calls #21539 leaves _meta empty (plugin must set it). This PR seeds _meta: { sessionID: ctx.sessionID } so every MCP server gets session context built-in, no plugin needed. Plugins can still extend it.

Net: this is #21539 + built-in sessionID. If this merges, #21539 can close.

cc @moghtader — credit to you for the plugin _meta hook approach, adopted here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Support _meta when calling downstream MCP tools Feature: Pass session identifier to MCP servers

1 participant