Skip to content

Commit e8eaa77

Browse files
committed
better mcp support - should fix hanging when streamable http server is added
1 parent a07f370 commit e8eaa77

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

opencode.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
}
2121
},
2222
"mcp": {
23+
"context7": {
24+
"type": "remote",
25+
"url": "https://mcp.context7.com/sse"
26+
},
2327
"weather": {
2428
"type": "local",
2529
"command": ["opencode", "x", "@h1deya/mcp-server-weather"]

packages/opencode/src/mcp/index.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { experimental_createMCPClient, type Tool } from "ai"
2-
import { Experimental_StdioMCPTransport } from "ai/mcp-stdio"
2+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
3+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"
4+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
35
import { App } from "../app/app"
46
import { Config } from "../config/config"
57
import { Log } from "../util/log"
@@ -32,15 +34,28 @@ export namespace MCP {
3234
}
3335
log.info("found", { key, type: mcp.type })
3436
if (mcp.type === "remote") {
35-
const client = await experimental_createMCPClient({
36-
name: key,
37-
transport: {
38-
type: "sse",
39-
url: mcp.url,
40-
headers: mcp.headers,
41-
},
42-
}).catch(() => {})
43-
if (!client) {
37+
const transports = [
38+
new StreamableHTTPClientTransport(new URL(mcp.url), {
39+
requestInit: {
40+
headers: mcp.headers,
41+
},
42+
}),
43+
new SSEClientTransport(new URL(mcp.url), {
44+
requestInit: {
45+
headers: mcp.headers,
46+
},
47+
}),
48+
]
49+
for (const transport of transports) {
50+
const client = await experimental_createMCPClient({
51+
name: key,
52+
transport,
53+
}).catch(() => {})
54+
if (!client) continue
55+
clients[key] = client
56+
break
57+
}
58+
if (!clients[key])
4459
Bus.publish(Session.Event.Error, {
4560
error: {
4661
name: "UnknownError",
@@ -49,16 +64,13 @@ export namespace MCP {
4964
},
5065
},
5166
})
52-
continue
53-
}
54-
clients[key] = client
5567
}
5668

5769
if (mcp.type === "local") {
5870
const [cmd, ...args] = mcp.command
5971
const client = await experimental_createMCPClient({
6072
name: key,
61-
transport: new Experimental_StdioMCPTransport({
73+
transport: new StdioClientTransport({
6274
stderr: "ignore",
6375
command: cmd,
6476
args,

0 commit comments

Comments
 (0)