From dd7db237a50ff753d515876839afc7f90001e5f9 Mon Sep 17 00:00:00 2001 From: Sathvik-1007 Date: Thu, 9 Apr 2026 10:51:38 +0530 Subject: [PATCH] feat(mcp): forward session ID and plugin _meta to MCP tool calls --- packages/opencode/src/mcp/index.ts | 4 +++- packages/opencode/src/session/prompt.ts | 8 ++++++-- packages/plugin/src/index.ts | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index 45e3e2567a28..6fd3d48aacf5 100644 --- a/packages/opencode/src/mcp/index.ts +++ b/packages/opencode/src/mcp/index.ts @@ -144,11 +144,13 @@ export namespace MCP { return dynamicTool({ description: mcpTool.description ?? "", inputSchema: jsonSchema(schema), - execute: async (args: unknown) => { + execute: async (args: unknown, opts?) => { + const meta = (opts as any)?._meta as Record | undefined return client.callTool( { name: mcpTool.name, arguments: (args || {}) as Record, + ...(meta ? { _meta: meta } : {}), }, CallToolResultSchema, { diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index e9bd5bcd5605..09fcf684880b 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -483,14 +483,18 @@ NOTE: At any point in time through this workflow you should feel free to ask the Effect.runPromise( Effect.gen(function* () { const ctx = context(args, opts) + const hook: { args: unknown; _meta?: Record } = { + args, + _meta: { sessionID: ctx.sessionID }, + } yield* plugin.trigger( "tool.execute.before", { tool: key, sessionID: ctx.sessionID, callID: opts.toolCallId }, - { args }, + hook, ) yield* Effect.promise(() => ctx.ask({ permission: key, metadata: {}, patterns: ["*"], always: ["*"] })) const result: Awaited>> = yield* Effect.promise(() => - execute(args, opts), + execute(hook.args, { ...opts, _meta: hook._meta } as typeof opts), ) yield* plugin.trigger( "tool.execute.after", diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index 1afb55daa76e..8e71f5ceec49 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -231,7 +231,7 @@ export interface Hooks { ) => Promise "tool.execute.before"?: ( input: { tool: string; sessionID: string; callID: string }, - output: { args: any }, + output: { args: any; _meta?: Record }, ) => Promise "shell.env"?: ( input: { cwd: string; sessionID?: string; callID?: string },