feat(opencode): add MCP server for delegating sessions to external agents - #41383
Open
harshnandwana wants to merge 2 commits into
Open
feat(opencode): add MCP server for delegating sessions to external agents#41383harshnandwana wants to merge 2 commits into
harshnandwana wants to merge 2 commits into
Conversation
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>
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #
Type of change
What does this PR do?
Adds an
opencode mcp-servercommand 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_sessionall 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 ofsdk.session.*,.wait()/.context()which aren't part of the SDK), referenced aServerAuth.token()function that was never defined, used an unimportedServertype, and used the deprecatedtool()overloads with zod schemas that failed typecheck because the MCP SDK vendors its ownzodversion. The second commit fixes all of that: switches tosdk.session.prompt()(which already blocks until the response completes, so no polling loop is needed), moves toregisterTool(), 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 typecheckpasses 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-tripscreate_session→list_sessions→get_session, including a not-found casebun run test— full opencode package suite, 3268 pass / 0 failpromptagainst a locally configured provider, to confirm the full tool-call → session-run → response path actually works, not just that it typechecksScreenshots / recordings
N/A — this is a CLI/server change with no UI.
Checklist