Skip to content

feat(opencode): add MCP server for delegating sessions to external agents - #41383

Open
harshnandwana wants to merge 2 commits into
anomalyco:devfrom
harshnandwana:feature/mcp-server
Open

feat(opencode): add MCP server for delegating sessions to external agents#41383
harshnandwana wants to merge 2 commits into
anomalyco:devfrom
harshnandwana:feature/mcp-server

Conversation

@harshnandwana

@harshnandwana harshnandwana commented Aug 9, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #

Type of change

  • New feature
  • Bug fix

What does this PR do?

Adds an opencode mcp-server command that exposes OpenCode sessions over the Model Context Protocol (stdio, streamable-http, or SSE), so external MCP clients (Claude Code, MCP Inspector, etc.) can create sessions and drive OpenCode's AI session runner directly. It's session-only: create_session, list_sessions, get_session, prompt, get_context, interrupt_session all delegate to real OpenCode sessions instead of exposing raw tools individually.

The first commit added the feature but had several bugs I found while testing it against a real client: it called SDK methods that don't exist (sdk.sessions.* instead of sdk.session.*, .wait()/.context() which aren't part of the SDK), referenced a ServerAuth.token() function that was never defined, used an unimported Server type, and used the deprecated tool() overloads with zod schemas that failed typecheck because the MCP SDK vendors its own zod version. The second commit fixes all of that: switches to sdk.session.prompt() (which already blocks until the response completes, so no polling loop is needed), moves to registerTool(), and wires the HTTP/SSE transports up to the same Basic-auth scheme (ServerAuth.header()) the rest of the server already uses instead of a bearer-token scheme that didn't exist anywhere else in the codebase.

How did you verify your code works?

  • bun run typecheck passes for the package and for the full monorepo (this also runs via the pre-push hook)
  • bun test test/mcp-server/mcp-server.test.ts — unit tests for the message-formatting helpers plus an integration test that spawns the real server over stdio and round-trips create_sessionlist_sessionsget_session, including a not-found case
  • bun run test — full opencode package suite, 3268 pass / 0 fail
  • Manually drove the server with a real MCP client end-to-end, including prompt against a locally configured provider, to confirm the full tool-call → session-run → response path actually works, not just that it typechecks

Screenshots / recordings

N/A — this is a CLI/server change with no UI.

Checklist

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

harshnandwana and others added 2 commits August 9, 2026 12:58
The initial MCP server implementation called nonexistent SDK methods
(sdk.sessions.* / .wait() / .context()), referenced an undefined
ServerAuth.token() API, used an unimported Server type, and had
deprecated-overload zod schemas that failed the type checker. Rewrites
tool handlers to use sdk.session.* correctly (prompt() already blocks
until completion), switches to registerTool with a compat cast for
the SDK's vendored zod version, and fixes HTTP/SSE transport auth to
use the project's existing Basic-auth ServerAuth.header().

Replaces the manual test script with real bun:test coverage for
formatParts/formatMessages and a stdio integration test exercising
the full tool lifecycle end-to-end.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

1 participant