chad-code 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -6
- package/package.json +36 -98
- package/AGENTS.md +0 -27
- package/Dockerfile +0 -18
- package/README.npm.md +0 -64
- package/bunfig.toml +0 -7
- package/eslint.config.js +0 -29
- package/parsers-config.ts +0 -253
- package/script/build.ts +0 -167
- package/script/publish-registries.ts +0 -187
- package/script/publish.ts +0 -93
- package/script/schema.ts +0 -47
- package/src/acp/README.md +0 -164
- package/src/acp/agent.ts +0 -1086
- package/src/acp/session.ts +0 -101
- package/src/acp/types.ts +0 -22
- package/src/agent/agent.ts +0 -253
- package/src/agent/generate.txt +0 -75
- package/src/agent/prompt/compaction.txt +0 -12
- package/src/agent/prompt/explore.txt +0 -18
- package/src/agent/prompt/summary.txt +0 -11
- package/src/agent/prompt/title.txt +0 -36
- package/src/auth/index.ts +0 -70
- package/src/bun/index.ts +0 -130
- package/src/bus/bus-event.ts +0 -43
- package/src/bus/global.ts +0 -10
- package/src/bus/index.ts +0 -105
- package/src/cli/bootstrap.ts +0 -17
- package/src/cli/cmd/acp.ts +0 -69
- package/src/cli/cmd/agent.ts +0 -257
- package/src/cli/cmd/auth.ts +0 -132
- package/src/cli/cmd/cmd.ts +0 -7
- package/src/cli/cmd/debug/agent.ts +0 -28
- package/src/cli/cmd/debug/config.ts +0 -15
- package/src/cli/cmd/debug/file.ts +0 -91
- package/src/cli/cmd/debug/index.ts +0 -45
- package/src/cli/cmd/debug/lsp.ts +0 -48
- package/src/cli/cmd/debug/ripgrep.ts +0 -83
- package/src/cli/cmd/debug/scrap.ts +0 -15
- package/src/cli/cmd/debug/skill.ts +0 -15
- package/src/cli/cmd/debug/snapshot.ts +0 -48
- package/src/cli/cmd/export.ts +0 -88
- package/src/cli/cmd/generate.ts +0 -38
- package/src/cli/cmd/github.ts +0 -32
- package/src/cli/cmd/import.ts +0 -98
- package/src/cli/cmd/mcp.ts +0 -670
- package/src/cli/cmd/models.ts +0 -42
- package/src/cli/cmd/pr.ts +0 -112
- package/src/cli/cmd/run.ts +0 -374
- package/src/cli/cmd/serve.ts +0 -16
- package/src/cli/cmd/session.ts +0 -135
- package/src/cli/cmd/stats.ts +0 -402
- package/src/cli/cmd/tui/app.tsx +0 -705
- package/src/cli/cmd/tui/attach.ts +0 -32
- package/src/cli/cmd/tui/component/border.tsx +0 -21
- package/src/cli/cmd/tui/component/dialog-agent.tsx +0 -31
- package/src/cli/cmd/tui/component/dialog-command.tsx +0 -124
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +0 -86
- package/src/cli/cmd/tui/component/dialog-model.tsx +0 -232
- package/src/cli/cmd/tui/component/dialog-provider.tsx +0 -228
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +0 -115
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +0 -31
- package/src/cli/cmd/tui/component/dialog-stash.tsx +0 -86
- package/src/cli/cmd/tui/component/dialog-status.tsx +0 -162
- package/src/cli/cmd/tui/component/dialog-tag.tsx +0 -44
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +0 -50
- package/src/cli/cmd/tui/component/did-you-know.tsx +0 -85
- package/src/cli/cmd/tui/component/logo.tsx +0 -43
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +0 -654
- package/src/cli/cmd/tui/component/prompt/history.tsx +0 -108
- package/src/cli/cmd/tui/component/prompt/index.tsx +0 -1078
- package/src/cli/cmd/tui/component/prompt/stash.tsx +0 -101
- package/src/cli/cmd/tui/component/textarea-keybindings.ts +0 -73
- package/src/cli/cmd/tui/component/tips.ts +0 -92
- package/src/cli/cmd/tui/component/todo-item.tsx +0 -32
- package/src/cli/cmd/tui/context/args.tsx +0 -14
- package/src/cli/cmd/tui/context/directory.ts +0 -13
- package/src/cli/cmd/tui/context/exit.tsx +0 -23
- package/src/cli/cmd/tui/context/helper.tsx +0 -25
- package/src/cli/cmd/tui/context/keybind.tsx +0 -101
- package/src/cli/cmd/tui/context/kv.tsx +0 -49
- package/src/cli/cmd/tui/context/local.tsx +0 -392
- package/src/cli/cmd/tui/context/prompt.tsx +0 -18
- package/src/cli/cmd/tui/context/route.tsx +0 -46
- package/src/cli/cmd/tui/context/sdk.tsx +0 -75
- package/src/cli/cmd/tui/context/sync.tsx +0 -384
- package/src/cli/cmd/tui/context/theme/aura.json +0 -69
- package/src/cli/cmd/tui/context/theme/ayu.json +0 -80
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +0 -233
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +0 -233
- package/src/cli/cmd/tui/context/theme/catppuccin.json +0 -112
- package/src/cli/cmd/tui/context/theme/chad.json +0 -245
- package/src/cli/cmd/tui/context/theme/cobalt2.json +0 -228
- package/src/cli/cmd/tui/context/theme/cursor.json +0 -249
- package/src/cli/cmd/tui/context/theme/dracula.json +0 -219
- package/src/cli/cmd/tui/context/theme/everforest.json +0 -241
- package/src/cli/cmd/tui/context/theme/flexoki.json +0 -237
- package/src/cli/cmd/tui/context/theme/github.json +0 -233
- package/src/cli/cmd/tui/context/theme/gruvbox.json +0 -95
- package/src/cli/cmd/tui/context/theme/kanagawa.json +0 -77
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +0 -227
- package/src/cli/cmd/tui/context/theme/material.json +0 -235
- package/src/cli/cmd/tui/context/theme/matrix.json +0 -77
- package/src/cli/cmd/tui/context/theme/mercury.json +0 -252
- package/src/cli/cmd/tui/context/theme/monokai.json +0 -221
- package/src/cli/cmd/tui/context/theme/nightowl.json +0 -221
- package/src/cli/cmd/tui/context/theme/nord.json +0 -223
- package/src/cli/cmd/tui/context/theme/one-dark.json +0 -84
- package/src/cli/cmd/tui/context/theme/orng.json +0 -245
- package/src/cli/cmd/tui/context/theme/osaka-jade.json +0 -93
- package/src/cli/cmd/tui/context/theme/palenight.json +0 -222
- package/src/cli/cmd/tui/context/theme/rosepine.json +0 -234
- package/src/cli/cmd/tui/context/theme/solarized.json +0 -223
- package/src/cli/cmd/tui/context/theme/synthwave84.json +0 -226
- package/src/cli/cmd/tui/context/theme/tokyonight.json +0 -243
- package/src/cli/cmd/tui/context/theme/vercel.json +0 -245
- package/src/cli/cmd/tui/context/theme/vesper.json +0 -218
- package/src/cli/cmd/tui/context/theme/zenburn.json +0 -223
- package/src/cli/cmd/tui/context/theme.tsx +0 -1137
- package/src/cli/cmd/tui/event.ts +0 -46
- package/src/cli/cmd/tui/routes/home.tsx +0 -138
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +0 -64
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +0 -109
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +0 -26
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +0 -47
- package/src/cli/cmd/tui/routes/session/footer.tsx +0 -88
- package/src/cli/cmd/tui/routes/session/header.tsx +0 -125
- package/src/cli/cmd/tui/routes/session/index.tsx +0 -1814
- package/src/cli/cmd/tui/routes/session/permission.tsx +0 -416
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +0 -318
- package/src/cli/cmd/tui/spawn.ts +0 -48
- package/src/cli/cmd/tui/thread.ts +0 -111
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +0 -57
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +0 -83
- package/src/cli/cmd/tui/ui/dialog-export-options.tsx +0 -204
- package/src/cli/cmd/tui/ui/dialog-help.tsx +0 -38
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +0 -77
- package/src/cli/cmd/tui/ui/dialog-select.tsx +0 -345
- package/src/cli/cmd/tui/ui/dialog.tsx +0 -171
- package/src/cli/cmd/tui/ui/link.tsx +0 -28
- package/src/cli/cmd/tui/ui/spinner.ts +0 -368
- package/src/cli/cmd/tui/ui/toast.tsx +0 -100
- package/src/cli/cmd/tui/util/clipboard.ts +0 -127
- package/src/cli/cmd/tui/util/editor.ts +0 -32
- package/src/cli/cmd/tui/util/signal.ts +0 -7
- package/src/cli/cmd/tui/util/terminal.ts +0 -114
- package/src/cli/cmd/tui/util/transcript.ts +0 -98
- package/src/cli/cmd/tui/worker.ts +0 -68
- package/src/cli/cmd/uninstall.ts +0 -344
- package/src/cli/cmd/upgrade.ts +0 -67
- package/src/cli/cmd/web.ts +0 -73
- package/src/cli/error.ts +0 -56
- package/src/cli/network.ts +0 -53
- package/src/cli/ui.ts +0 -87
- package/src/cli/upgrade.ts +0 -25
- package/src/command/index.ts +0 -131
- package/src/command/template/initialize.txt +0 -10
- package/src/command/template/review.txt +0 -97
- package/src/config/config.ts +0 -1124
- package/src/config/markdown.ts +0 -41
- package/src/env/index.ts +0 -26
- package/src/file/ignore.ts +0 -83
- package/src/file/index.ts +0 -411
- package/src/file/ripgrep.ts +0 -402
- package/src/file/time.ts +0 -64
- package/src/file/watcher.ts +0 -117
- package/src/flag/flag.ts +0 -52
- package/src/format/formatter.ts +0 -359
- package/src/format/index.ts +0 -137
- package/src/global/index.ts +0 -55
- package/src/id/id.ts +0 -73
- package/src/ide/index.ts +0 -77
- package/src/index.ts +0 -159
- package/src/installation/index.ts +0 -198
- package/src/lsp/client.ts +0 -252
- package/src/lsp/index.ts +0 -485
- package/src/lsp/language.ts +0 -119
- package/src/lsp/server.ts +0 -2023
- package/src/mcp/auth.ts +0 -135
- package/src/mcp/index.ts +0 -874
- package/src/mcp/oauth-callback.ts +0 -200
- package/src/mcp/oauth-provider.ts +0 -154
- package/src/patch/index.ts +0 -622
- package/src/permission/arity.ts +0 -163
- package/src/permission/index.ts +0 -210
- package/src/permission/next.ts +0 -268
- package/src/plugin/index.ts +0 -106
- package/src/project/bootstrap.ts +0 -31
- package/src/project/instance.ts +0 -78
- package/src/project/project.ts +0 -263
- package/src/project/state.ts +0 -65
- package/src/project/vcs.ts +0 -76
- package/src/provider/auth.ts +0 -143
- package/src/provider/models-macro.ts +0 -4
- package/src/provider/models.ts +0 -77
- package/src/provider/provider.ts +0 -516
- package/src/provider/transform.ts +0 -114
- package/src/pty/index.ts +0 -212
- package/src/server/error.ts +0 -36
- package/src/server/mdns.ts +0 -57
- package/src/server/project.ts +0 -79
- package/src/server/server.ts +0 -2866
- package/src/server/tui.ts +0 -71
- package/src/session/compaction.ts +0 -225
- package/src/session/index.ts +0 -469
- package/src/session/llm.ts +0 -213
- package/src/session/message-v2.ts +0 -742
- package/src/session/message.ts +0 -189
- package/src/session/processor.ts +0 -402
- package/src/session/prompt/anthropic-20250930.txt +0 -166
- package/src/session/prompt/anthropic.txt +0 -105
- package/src/session/prompt/anthropic_spoof.txt +0 -1
- package/src/session/prompt/beast.txt +0 -147
- package/src/session/prompt/build-switch.txt +0 -5
- package/src/session/prompt/codex.txt +0 -318
- package/src/session/prompt/copilot-gpt-5.txt +0 -143
- package/src/session/prompt/gemini.txt +0 -155
- package/src/session/prompt/max-steps.txt +0 -16
- package/src/session/prompt/plan-reminder-anthropic.txt +0 -67
- package/src/session/prompt/plan.txt +0 -26
- package/src/session/prompt/qwen.txt +0 -109
- package/src/session/prompt.ts +0 -1621
- package/src/session/retry.ts +0 -90
- package/src/session/revert.ts +0 -108
- package/src/session/status.ts +0 -76
- package/src/session/summary.ts +0 -194
- package/src/session/system.ts +0 -108
- package/src/session/todo.ts +0 -37
- package/src/share/share-next.ts +0 -194
- package/src/share/share.ts +0 -23
- package/src/shell/shell.ts +0 -67
- package/src/skill/index.ts +0 -1
- package/src/skill/skill.ts +0 -124
- package/src/snapshot/index.ts +0 -197
- package/src/storage/storage.ts +0 -226
- package/src/tool/bash.ts +0 -262
- package/src/tool/bash.txt +0 -116
- package/src/tool/batch.ts +0 -175
- package/src/tool/batch.txt +0 -24
- package/src/tool/codesearch.ts +0 -132
- package/src/tool/codesearch.txt +0 -12
- package/src/tool/edit.ts +0 -655
- package/src/tool/edit.txt +0 -10
- package/src/tool/glob.ts +0 -75
- package/src/tool/glob.txt +0 -6
- package/src/tool/grep.ts +0 -132
- package/src/tool/grep.txt +0 -8
- package/src/tool/invalid.ts +0 -17
- package/src/tool/ls.ts +0 -119
- package/src/tool/ls.txt +0 -1
- package/src/tool/lsp.ts +0 -94
- package/src/tool/lsp.txt +0 -19
- package/src/tool/multiedit.ts +0 -46
- package/src/tool/multiedit.txt +0 -41
- package/src/tool/patch.ts +0 -210
- package/src/tool/patch.txt +0 -1
- package/src/tool/read.ts +0 -191
- package/src/tool/read.txt +0 -12
- package/src/tool/registry.ts +0 -137
- package/src/tool/skill.ts +0 -77
- package/src/tool/task.ts +0 -167
- package/src/tool/task.txt +0 -60
- package/src/tool/todo.ts +0 -53
- package/src/tool/todoread.txt +0 -14
- package/src/tool/todowrite.txt +0 -167
- package/src/tool/tool.ts +0 -73
- package/src/tool/webfetch.ts +0 -182
- package/src/tool/webfetch.txt +0 -13
- package/src/tool/websearch.ts +0 -144
- package/src/tool/websearch.txt +0 -11
- package/src/tool/write.ts +0 -84
- package/src/tool/write.txt +0 -8
- package/src/util/archive.ts +0 -16
- package/src/util/color.ts +0 -19
- package/src/util/context.ts +0 -25
- package/src/util/defer.ts +0 -12
- package/src/util/eventloop.ts +0 -20
- package/src/util/filesystem.ts +0 -83
- package/src/util/fn.ts +0 -11
- package/src/util/iife.ts +0 -3
- package/src/util/keybind.ts +0 -102
- package/src/util/lazy.ts +0 -18
- package/src/util/locale.ts +0 -81
- package/src/util/lock.ts +0 -98
- package/src/util/log.ts +0 -180
- package/src/util/queue.ts +0 -32
- package/src/util/rpc.ts +0 -42
- package/src/util/scrap.ts +0 -10
- package/src/util/signal.ts +0 -12
- package/src/util/timeout.ts +0 -14
- package/src/util/token.ts +0 -7
- package/src/util/wildcard.ts +0 -54
- package/src/worktree/index.ts +0 -217
- package/sst-env.d.ts +0 -9
- package/test/agent/agent.test.ts +0 -448
- package/test/bun.test.ts +0 -53
- package/test/cli/github-action.test.ts +0 -129
- package/test/cli/github-remote.test.ts +0 -80
- package/test/cli/tui/transcript.test.ts +0 -297
- package/test/config/agent-color.test.ts +0 -66
- package/test/config/config.test.ts +0 -870
- package/test/config/markdown.test.ts +0 -89
- package/test/file/ignore.test.ts +0 -10
- package/test/file/path-traversal.test.ts +0 -115
- package/test/fixture/fixture.ts +0 -45
- package/test/fixture/lsp/fake-lsp-server.js +0 -77
- package/test/ide/ide.test.ts +0 -82
- package/test/keybind.test.ts +0 -421
- package/test/lsp/client.test.ts +0 -95
- package/test/mcp/headers.test.ts +0 -153
- package/test/patch/patch.test.ts +0 -348
- package/test/permission/arity.test.ts +0 -33
- package/test/permission/next.test.ts +0 -652
- package/test/preload.ts +0 -63
- package/test/project/project.test.ts +0 -120
- package/test/provider/amazon-bedrock.test.ts +0 -236
- package/test/provider/provider.test.ts +0 -2127
- package/test/provider/transform.test.ts +0 -980
- package/test/server/session-select.test.ts +0 -78
- package/test/session/compaction.test.ts +0 -251
- package/test/session/message-v2.test.ts +0 -570
- package/test/session/retry.test.ts +0 -131
- package/test/session/revert-compact.test.ts +0 -285
- package/test/session/session.test.ts +0 -71
- package/test/skill/skill.test.ts +0 -185
- package/test/snapshot/snapshot.test.ts +0 -939
- package/test/tool/__snapshots__/tool.test.ts.snap +0 -9
- package/test/tool/bash.test.ts +0 -232
- package/test/tool/grep.test.ts +0 -109
- package/test/tool/patch.test.ts +0 -261
- package/test/tool/read.test.ts +0 -167
- package/test/util/iife.test.ts +0 -36
- package/test/util/lazy.test.ts +0 -50
- package/test/util/timeout.test.ts +0 -21
- package/test/util/wildcard.test.ts +0 -55
- package/tsconfig.json +0 -16
- /package/{script/postinstall.mjs → postinstall.mjs} +0 -0
package/src/mcp/index.ts
DELETED
|
@@ -1,874 +0,0 @@
|
|
|
1
|
-
import { dynamicTool, type Tool, jsonSchema, type JSONSchema7 } from "ai"
|
|
2
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
3
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
|
4
|
-
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"
|
|
5
|
-
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
|
|
6
|
-
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js"
|
|
7
|
-
import {
|
|
8
|
-
CallToolResultSchema,
|
|
9
|
-
type Tool as MCPToolDef,
|
|
10
|
-
ToolListChangedNotificationSchema,
|
|
11
|
-
} from "@modelcontextprotocol/sdk/types.js"
|
|
12
|
-
import { Config } from "../config/config"
|
|
13
|
-
import { Log } from "../util/log"
|
|
14
|
-
import { NamedError } from "@opencode-ai/util/error"
|
|
15
|
-
import z from "zod/v4"
|
|
16
|
-
import { Instance } from "../project/instance"
|
|
17
|
-
import { Installation } from "../installation"
|
|
18
|
-
import { withTimeout } from "@/util/timeout"
|
|
19
|
-
import { McpOAuthProvider } from "./oauth-provider"
|
|
20
|
-
import { McpOAuthCallback } from "./oauth-callback"
|
|
21
|
-
import { McpAuth } from "./auth"
|
|
22
|
-
import { BusEvent } from "../bus/bus-event"
|
|
23
|
-
import { Bus } from "@/bus"
|
|
24
|
-
import { TuiEvent } from "@/cli/cmd/tui/event"
|
|
25
|
-
import open from "open"
|
|
26
|
-
|
|
27
|
-
export namespace MCP {
|
|
28
|
-
const log = Log.create({ service: "mcp" })
|
|
29
|
-
const DEFAULT_TIMEOUT = 30_000
|
|
30
|
-
|
|
31
|
-
export const Resource = z
|
|
32
|
-
.object({
|
|
33
|
-
name: z.string(),
|
|
34
|
-
uri: z.string(),
|
|
35
|
-
description: z.string().optional(),
|
|
36
|
-
mimeType: z.string().optional(),
|
|
37
|
-
client: z.string(),
|
|
38
|
-
})
|
|
39
|
-
.meta({ ref: "McpResource" })
|
|
40
|
-
export type Resource = z.infer<typeof Resource>
|
|
41
|
-
|
|
42
|
-
export const ToolsChanged = BusEvent.define(
|
|
43
|
-
"mcp.tools.changed",
|
|
44
|
-
z.object({
|
|
45
|
-
server: z.string(),
|
|
46
|
-
}),
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
export const Failed = NamedError.create(
|
|
50
|
-
"MCPFailed",
|
|
51
|
-
z.object({
|
|
52
|
-
name: z.string(),
|
|
53
|
-
}),
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
type MCPClient = Client
|
|
57
|
-
|
|
58
|
-
export const Status = z
|
|
59
|
-
.discriminatedUnion("status", [
|
|
60
|
-
z
|
|
61
|
-
.object({
|
|
62
|
-
status: z.literal("connected"),
|
|
63
|
-
})
|
|
64
|
-
.meta({
|
|
65
|
-
ref: "MCPStatusConnected",
|
|
66
|
-
}),
|
|
67
|
-
z
|
|
68
|
-
.object({
|
|
69
|
-
status: z.literal("disabled"),
|
|
70
|
-
})
|
|
71
|
-
.meta({
|
|
72
|
-
ref: "MCPStatusDisabled",
|
|
73
|
-
}),
|
|
74
|
-
z
|
|
75
|
-
.object({
|
|
76
|
-
status: z.literal("failed"),
|
|
77
|
-
error: z.string(),
|
|
78
|
-
})
|
|
79
|
-
.meta({
|
|
80
|
-
ref: "MCPStatusFailed",
|
|
81
|
-
}),
|
|
82
|
-
z
|
|
83
|
-
.object({
|
|
84
|
-
status: z.literal("needs_auth"),
|
|
85
|
-
})
|
|
86
|
-
.meta({
|
|
87
|
-
ref: "MCPStatusNeedsAuth",
|
|
88
|
-
}),
|
|
89
|
-
z
|
|
90
|
-
.object({
|
|
91
|
-
status: z.literal("needs_client_registration"),
|
|
92
|
-
error: z.string(),
|
|
93
|
-
})
|
|
94
|
-
.meta({
|
|
95
|
-
ref: "MCPStatusNeedsClientRegistration",
|
|
96
|
-
}),
|
|
97
|
-
])
|
|
98
|
-
.meta({
|
|
99
|
-
ref: "MCPStatus",
|
|
100
|
-
})
|
|
101
|
-
export type Status = z.infer<typeof Status>
|
|
102
|
-
|
|
103
|
-
// Register notification handlers for MCP client
|
|
104
|
-
function registerNotificationHandlers(client: MCPClient, serverName: string) {
|
|
105
|
-
client.setNotificationHandler(ToolListChangedNotificationSchema, async () => {
|
|
106
|
-
log.info("tools list changed notification received", { server: serverName })
|
|
107
|
-
Bus.publish(ToolsChanged, { server: serverName })
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Convert MCP tool definition to AI SDK Tool type
|
|
112
|
-
async function convertMcpTool(mcpTool: MCPToolDef, client: MCPClient): Promise<Tool> {
|
|
113
|
-
const inputSchema = mcpTool.inputSchema
|
|
114
|
-
|
|
115
|
-
// Spread first, then override type to ensure it's always "object"
|
|
116
|
-
const schema: JSONSchema7 = {
|
|
117
|
-
...(inputSchema as JSONSchema7),
|
|
118
|
-
type: "object",
|
|
119
|
-
properties: (inputSchema.properties ?? {}) as JSONSchema7["properties"],
|
|
120
|
-
additionalProperties: false,
|
|
121
|
-
}
|
|
122
|
-
const config = await Config.get()
|
|
123
|
-
|
|
124
|
-
return dynamicTool({
|
|
125
|
-
description: mcpTool.description ?? "",
|
|
126
|
-
inputSchema: jsonSchema(schema),
|
|
127
|
-
execute: async (args: unknown) => {
|
|
128
|
-
return client.callTool(
|
|
129
|
-
{
|
|
130
|
-
name: mcpTool.name,
|
|
131
|
-
arguments: args as Record<string, unknown>,
|
|
132
|
-
},
|
|
133
|
-
CallToolResultSchema,
|
|
134
|
-
{
|
|
135
|
-
resetTimeoutOnProgress: true,
|
|
136
|
-
timeout: config.experimental?.mcp_timeout,
|
|
137
|
-
},
|
|
138
|
-
)
|
|
139
|
-
},
|
|
140
|
-
})
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Store transports for OAuth servers to allow finishing auth
|
|
144
|
-
type TransportWithAuth = StreamableHTTPClientTransport | SSEClientTransport
|
|
145
|
-
const pendingOAuthTransports = new Map<string, TransportWithAuth>()
|
|
146
|
-
|
|
147
|
-
// Prompt cache types
|
|
148
|
-
type PromptInfo = Awaited<ReturnType<MCPClient["listPrompts"]>>["prompts"][number]
|
|
149
|
-
|
|
150
|
-
type ResourceInfo = Awaited<ReturnType<MCPClient["listResources"]>>["resources"][number]
|
|
151
|
-
type McpEntry = NonNullable<Config.Info["mcp"]>[string]
|
|
152
|
-
function isMcpConfigured(entry: McpEntry): entry is Config.Mcp {
|
|
153
|
-
return typeof entry === "object" && entry !== null && "type" in entry
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const state = Instance.state(
|
|
157
|
-
async () => {
|
|
158
|
-
const cfg = await Config.get()
|
|
159
|
-
const config = cfg.mcp ?? {}
|
|
160
|
-
const clients: Record<string, MCPClient> = {}
|
|
161
|
-
const status: Record<string, Status> = {}
|
|
162
|
-
|
|
163
|
-
await Promise.all(
|
|
164
|
-
Object.entries(config).map(async ([key, mcp]) => {
|
|
165
|
-
if (!isMcpConfigured(mcp)) {
|
|
166
|
-
log.error("Ignoring MCP config entry without type", { key })
|
|
167
|
-
return
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// If disabled by config, mark as disabled without trying to connect
|
|
171
|
-
if (mcp.enabled === false) {
|
|
172
|
-
status[key] = { status: "disabled" }
|
|
173
|
-
return
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const result = await create(key, mcp).catch(() => undefined)
|
|
177
|
-
if (!result) return
|
|
178
|
-
|
|
179
|
-
status[key] = result.status
|
|
180
|
-
|
|
181
|
-
if (result.mcpClient) {
|
|
182
|
-
clients[key] = result.mcpClient
|
|
183
|
-
}
|
|
184
|
-
}),
|
|
185
|
-
)
|
|
186
|
-
return {
|
|
187
|
-
status,
|
|
188
|
-
clients,
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
async (state) => {
|
|
192
|
-
await Promise.all(
|
|
193
|
-
Object.values(state.clients).map((client) =>
|
|
194
|
-
client.close().catch((error) => {
|
|
195
|
-
log.error("Failed to close MCP client", {
|
|
196
|
-
error,
|
|
197
|
-
})
|
|
198
|
-
}),
|
|
199
|
-
),
|
|
200
|
-
)
|
|
201
|
-
pendingOAuthTransports.clear()
|
|
202
|
-
},
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
// Helper function to fetch prompts for a specific client
|
|
206
|
-
async function fetchPromptsForClient(clientName: string, client: Client) {
|
|
207
|
-
const prompts = await client.listPrompts().catch((e) => {
|
|
208
|
-
log.error("failed to get prompts", { clientName, error: e.message })
|
|
209
|
-
return undefined
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
if (!prompts) {
|
|
213
|
-
return
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const commands: Record<string, PromptInfo & { client: string }> = {}
|
|
217
|
-
|
|
218
|
-
for (const prompt of prompts.prompts) {
|
|
219
|
-
const sanitizedClientName = clientName.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
220
|
-
const sanitizedPromptName = prompt.name.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
221
|
-
const key = sanitizedClientName + ":" + sanitizedPromptName
|
|
222
|
-
|
|
223
|
-
commands[key] = { ...prompt, client: clientName }
|
|
224
|
-
}
|
|
225
|
-
return commands
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async function fetchResourcesForClient(clientName: string, client: Client) {
|
|
229
|
-
const resources = await client.listResources().catch((e) => {
|
|
230
|
-
log.error("failed to get prompts", { clientName, error: e.message })
|
|
231
|
-
return undefined
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
if (!resources) {
|
|
235
|
-
return
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const commands: Record<string, ResourceInfo & { client: string }> = {}
|
|
239
|
-
|
|
240
|
-
for (const resource of resources.resources) {
|
|
241
|
-
const sanitizedClientName = clientName.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
242
|
-
const sanitizedResourceName = resource.name.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
243
|
-
const key = sanitizedClientName + ":" + sanitizedResourceName
|
|
244
|
-
|
|
245
|
-
commands[key] = { ...resource, client: clientName }
|
|
246
|
-
}
|
|
247
|
-
return commands
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export async function add(name: string, mcp: Config.Mcp) {
|
|
251
|
-
const s = await state()
|
|
252
|
-
const result = await create(name, mcp)
|
|
253
|
-
if (!result) {
|
|
254
|
-
const status = {
|
|
255
|
-
status: "failed" as const,
|
|
256
|
-
error: "unknown error",
|
|
257
|
-
}
|
|
258
|
-
s.status[name] = status
|
|
259
|
-
return {
|
|
260
|
-
status,
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
if (!result.mcpClient) {
|
|
264
|
-
s.status[name] = result.status
|
|
265
|
-
return {
|
|
266
|
-
status: s.status,
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
s.clients[name] = result.mcpClient
|
|
270
|
-
s.status[name] = result.status
|
|
271
|
-
|
|
272
|
-
return {
|
|
273
|
-
status: s.status,
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async function create(key: string, mcp: Config.Mcp) {
|
|
278
|
-
if (mcp.enabled === false) {
|
|
279
|
-
log.info("mcp server disabled", { key })
|
|
280
|
-
return {
|
|
281
|
-
mcpClient: undefined,
|
|
282
|
-
status: { status: "disabled" as const },
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
log.info("found", { key, type: mcp.type })
|
|
287
|
-
let mcpClient: MCPClient | undefined
|
|
288
|
-
let status: Status | undefined = undefined
|
|
289
|
-
|
|
290
|
-
if (mcp.type === "remote") {
|
|
291
|
-
// OAuth is enabled by default for remote servers unless explicitly disabled with oauth: false
|
|
292
|
-
const oauthDisabled = mcp.oauth === false
|
|
293
|
-
const oauthConfig = typeof mcp.oauth === "object" ? mcp.oauth : undefined
|
|
294
|
-
let authProvider: McpOAuthProvider | undefined
|
|
295
|
-
|
|
296
|
-
if (!oauthDisabled) {
|
|
297
|
-
authProvider = new McpOAuthProvider(
|
|
298
|
-
key,
|
|
299
|
-
mcp.url,
|
|
300
|
-
{
|
|
301
|
-
clientId: oauthConfig?.clientId,
|
|
302
|
-
clientSecret: oauthConfig?.clientSecret,
|
|
303
|
-
scope: oauthConfig?.scope,
|
|
304
|
-
},
|
|
305
|
-
{
|
|
306
|
-
onRedirect: async (url) => {
|
|
307
|
-
log.info("oauth redirect requested", { key, url: url.toString() })
|
|
308
|
-
// Store the URL - actual browser opening is handled by startAuth
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
)
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const transports: Array<{ name: string; transport: TransportWithAuth }> = [
|
|
315
|
-
{
|
|
316
|
-
name: "StreamableHTTP",
|
|
317
|
-
transport: new StreamableHTTPClientTransport(new URL(mcp.url), {
|
|
318
|
-
authProvider,
|
|
319
|
-
requestInit: mcp.headers ? { headers: mcp.headers } : undefined,
|
|
320
|
-
}),
|
|
321
|
-
},
|
|
322
|
-
{
|
|
323
|
-
name: "SSE",
|
|
324
|
-
transport: new SSEClientTransport(new URL(mcp.url), {
|
|
325
|
-
authProvider,
|
|
326
|
-
requestInit: mcp.headers ? { headers: mcp.headers } : undefined,
|
|
327
|
-
}),
|
|
328
|
-
},
|
|
329
|
-
]
|
|
330
|
-
|
|
331
|
-
let lastError: Error | undefined
|
|
332
|
-
const connectTimeout = mcp.timeout ?? DEFAULT_TIMEOUT
|
|
333
|
-
for (const { name, transport } of transports) {
|
|
334
|
-
try {
|
|
335
|
-
const client = new Client({
|
|
336
|
-
name: "opencode",
|
|
337
|
-
version: Installation.VERSION,
|
|
338
|
-
})
|
|
339
|
-
await withTimeout(client.connect(transport), connectTimeout)
|
|
340
|
-
registerNotificationHandlers(client, key)
|
|
341
|
-
mcpClient = client
|
|
342
|
-
log.info("connected", { key, transport: name })
|
|
343
|
-
status = { status: "connected" }
|
|
344
|
-
break
|
|
345
|
-
} catch (error) {
|
|
346
|
-
lastError = error instanceof Error ? error : new Error(String(error))
|
|
347
|
-
|
|
348
|
-
// Handle OAuth-specific errors
|
|
349
|
-
if (error instanceof UnauthorizedError) {
|
|
350
|
-
log.info("mcp server requires authentication", { key, transport: name })
|
|
351
|
-
|
|
352
|
-
// Check if this is a "needs registration" error
|
|
353
|
-
if (lastError.message.includes("registration") || lastError.message.includes("client_id")) {
|
|
354
|
-
status = {
|
|
355
|
-
status: "needs_client_registration" as const,
|
|
356
|
-
error: "Server does not support dynamic client registration. Please provide clientId in config.",
|
|
357
|
-
}
|
|
358
|
-
// Show toast for needs_client_registration
|
|
359
|
-
Bus.publish(TuiEvent.ToastShow, {
|
|
360
|
-
title: "MCP Authentication Required",
|
|
361
|
-
message: `Server "${key}" requires a pre-registered client ID. Add clientId to your config.`,
|
|
362
|
-
variant: "warning",
|
|
363
|
-
duration: 8000,
|
|
364
|
-
}).catch((e) => log.debug("failed to show toast", { error: e }))
|
|
365
|
-
} else {
|
|
366
|
-
// Store transport for later finishAuth call
|
|
367
|
-
pendingOAuthTransports.set(key, transport)
|
|
368
|
-
status = { status: "needs_auth" as const }
|
|
369
|
-
// Show toast for needs_auth
|
|
370
|
-
Bus.publish(TuiEvent.ToastShow, {
|
|
371
|
-
title: "MCP Authentication Required",
|
|
372
|
-
message: `Server "${key}" requires authentication. Run: opencode mcp auth ${key}`,
|
|
373
|
-
variant: "warning",
|
|
374
|
-
duration: 8000,
|
|
375
|
-
}).catch((e) => log.debug("failed to show toast", { error: e }))
|
|
376
|
-
}
|
|
377
|
-
break
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
log.debug("transport connection failed", {
|
|
381
|
-
key,
|
|
382
|
-
transport: name,
|
|
383
|
-
url: mcp.url,
|
|
384
|
-
error: lastError.message,
|
|
385
|
-
})
|
|
386
|
-
status = {
|
|
387
|
-
status: "failed" as const,
|
|
388
|
-
error: lastError.message,
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
if (mcp.type === "local") {
|
|
395
|
-
const [cmd, ...args] = mcp.command
|
|
396
|
-
const cwd = Instance.directory
|
|
397
|
-
const transport = new StdioClientTransport({
|
|
398
|
-
stderr: "ignore",
|
|
399
|
-
command: cmd,
|
|
400
|
-
args,
|
|
401
|
-
cwd,
|
|
402
|
-
env: {
|
|
403
|
-
...process.env,
|
|
404
|
-
...(cmd === "opencode" ? { BUN_BE_BUN: "1" } : {}),
|
|
405
|
-
...mcp.environment,
|
|
406
|
-
},
|
|
407
|
-
})
|
|
408
|
-
|
|
409
|
-
const connectTimeout = mcp.timeout ?? DEFAULT_TIMEOUT
|
|
410
|
-
try {
|
|
411
|
-
const client = new Client({
|
|
412
|
-
name: "opencode",
|
|
413
|
-
version: Installation.VERSION,
|
|
414
|
-
})
|
|
415
|
-
await withTimeout(client.connect(transport), connectTimeout)
|
|
416
|
-
registerNotificationHandlers(client, key)
|
|
417
|
-
mcpClient = client
|
|
418
|
-
status = {
|
|
419
|
-
status: "connected",
|
|
420
|
-
}
|
|
421
|
-
} catch (error) {
|
|
422
|
-
log.error("local mcp startup failed", {
|
|
423
|
-
key,
|
|
424
|
-
command: mcp.command,
|
|
425
|
-
cwd,
|
|
426
|
-
error: error instanceof Error ? error.message : String(error),
|
|
427
|
-
})
|
|
428
|
-
status = {
|
|
429
|
-
status: "failed" as const,
|
|
430
|
-
error: error instanceof Error ? error.message : String(error),
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
if (!status) {
|
|
436
|
-
status = {
|
|
437
|
-
status: "failed" as const,
|
|
438
|
-
error: "Unknown error",
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
if (!mcpClient) {
|
|
443
|
-
return {
|
|
444
|
-
mcpClient: undefined,
|
|
445
|
-
status,
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
const result = await withTimeout(mcpClient.listTools(), mcp.timeout ?? DEFAULT_TIMEOUT).catch((err) => {
|
|
450
|
-
log.error("failed to get tools from client", { key, error: err })
|
|
451
|
-
return undefined
|
|
452
|
-
})
|
|
453
|
-
if (!result) {
|
|
454
|
-
await mcpClient.close().catch((error) => {
|
|
455
|
-
log.error("Failed to close MCP client", {
|
|
456
|
-
error,
|
|
457
|
-
})
|
|
458
|
-
})
|
|
459
|
-
status = {
|
|
460
|
-
status: "failed",
|
|
461
|
-
error: "Failed to get tools",
|
|
462
|
-
}
|
|
463
|
-
return {
|
|
464
|
-
mcpClient: undefined,
|
|
465
|
-
status: {
|
|
466
|
-
status: "failed" as const,
|
|
467
|
-
error: "Failed to get tools",
|
|
468
|
-
},
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
log.info("create() successfully created client", { key, toolCount: result.tools.length })
|
|
473
|
-
return {
|
|
474
|
-
mcpClient,
|
|
475
|
-
status,
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
export async function status() {
|
|
480
|
-
const s = await state()
|
|
481
|
-
const cfg = await Config.get()
|
|
482
|
-
const config = cfg.mcp ?? {}
|
|
483
|
-
const result: Record<string, Status> = {}
|
|
484
|
-
|
|
485
|
-
// Include all configured MCPs from config, not just connected ones
|
|
486
|
-
for (const [key, mcp] of Object.entries(config)) {
|
|
487
|
-
if (!isMcpConfigured(mcp)) continue
|
|
488
|
-
result[key] = s.status[key] ?? { status: "disabled" }
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
return result
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
export async function clients() {
|
|
495
|
-
return state().then((state) => state.clients)
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
export async function connect(name: string) {
|
|
499
|
-
const cfg = await Config.get()
|
|
500
|
-
const config = cfg.mcp ?? {}
|
|
501
|
-
const mcp = config[name]
|
|
502
|
-
if (!mcp) {
|
|
503
|
-
log.error("MCP config not found", { name })
|
|
504
|
-
return
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
if (!isMcpConfigured(mcp)) {
|
|
508
|
-
log.error("Ignoring MCP connect request for config without type", { name })
|
|
509
|
-
return
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const result = await create(name, { ...mcp, enabled: true })
|
|
513
|
-
|
|
514
|
-
if (!result) {
|
|
515
|
-
const s = await state()
|
|
516
|
-
s.status[name] = {
|
|
517
|
-
status: "failed",
|
|
518
|
-
error: "Unknown error during connection",
|
|
519
|
-
}
|
|
520
|
-
return
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
const s = await state()
|
|
524
|
-
s.status[name] = result.status
|
|
525
|
-
if (result.mcpClient) {
|
|
526
|
-
s.clients[name] = result.mcpClient
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
export async function disconnect(name: string) {
|
|
531
|
-
const s = await state()
|
|
532
|
-
const client = s.clients[name]
|
|
533
|
-
if (client) {
|
|
534
|
-
await client.close().catch((error) => {
|
|
535
|
-
log.error("Failed to close MCP client", { name, error })
|
|
536
|
-
})
|
|
537
|
-
delete s.clients[name]
|
|
538
|
-
}
|
|
539
|
-
s.status[name] = { status: "disabled" }
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
export async function tools() {
|
|
543
|
-
const result: Record<string, Tool> = {}
|
|
544
|
-
const s = await state()
|
|
545
|
-
const clientsSnapshot = await clients()
|
|
546
|
-
|
|
547
|
-
for (const [clientName, client] of Object.entries(clientsSnapshot)) {
|
|
548
|
-
// Only include tools from connected MCPs (skip disabled ones)
|
|
549
|
-
if (s.status[clientName]?.status !== "connected") {
|
|
550
|
-
continue
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
const toolsResult = await client.listTools().catch((e) => {
|
|
554
|
-
log.error("failed to get tools", { clientName, error: e.message })
|
|
555
|
-
const failedStatus = {
|
|
556
|
-
status: "failed" as const,
|
|
557
|
-
error: e instanceof Error ? e.message : String(e),
|
|
558
|
-
}
|
|
559
|
-
s.status[clientName] = failedStatus
|
|
560
|
-
delete s.clients[clientName]
|
|
561
|
-
return undefined
|
|
562
|
-
})
|
|
563
|
-
if (!toolsResult) {
|
|
564
|
-
continue
|
|
565
|
-
}
|
|
566
|
-
for (const mcpTool of toolsResult.tools) {
|
|
567
|
-
const sanitizedClientName = clientName.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
568
|
-
const sanitizedToolName = mcpTool.name.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
569
|
-
result[sanitizedClientName + "_" + sanitizedToolName] = await convertMcpTool(mcpTool, client)
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
return result
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
export async function prompts() {
|
|
576
|
-
const s = await state()
|
|
577
|
-
const clientsSnapshot = await clients()
|
|
578
|
-
|
|
579
|
-
const prompts = Object.fromEntries<PromptInfo & { client: string }>(
|
|
580
|
-
(
|
|
581
|
-
await Promise.all(
|
|
582
|
-
Object.entries(clientsSnapshot).map(async ([clientName, client]) => {
|
|
583
|
-
if (s.status[clientName]?.status !== "connected") {
|
|
584
|
-
return []
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
return Object.entries((await fetchPromptsForClient(clientName, client)) ?? {})
|
|
588
|
-
}),
|
|
589
|
-
)
|
|
590
|
-
).flat(),
|
|
591
|
-
)
|
|
592
|
-
|
|
593
|
-
return prompts
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
export async function resources() {
|
|
597
|
-
const s = await state()
|
|
598
|
-
const clientsSnapshot = await clients()
|
|
599
|
-
|
|
600
|
-
const result = Object.fromEntries<ResourceInfo & { client: string }>(
|
|
601
|
-
(
|
|
602
|
-
await Promise.all(
|
|
603
|
-
Object.entries(clientsSnapshot).map(async ([clientName, client]) => {
|
|
604
|
-
if (s.status[clientName]?.status !== "connected") {
|
|
605
|
-
return []
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
return Object.entries((await fetchResourcesForClient(clientName, client)) ?? {})
|
|
609
|
-
}),
|
|
610
|
-
)
|
|
611
|
-
).flat(),
|
|
612
|
-
)
|
|
613
|
-
|
|
614
|
-
return result
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
export async function getPrompt(clientName: string, name: string, args?: Record<string, string>) {
|
|
618
|
-
const clientsSnapshot = await clients()
|
|
619
|
-
const client = clientsSnapshot[clientName]
|
|
620
|
-
|
|
621
|
-
if (!client) {
|
|
622
|
-
log.warn("client not found for prompt", {
|
|
623
|
-
clientName,
|
|
624
|
-
})
|
|
625
|
-
return undefined
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
const result = await client
|
|
629
|
-
.getPrompt({
|
|
630
|
-
name: name,
|
|
631
|
-
arguments: args,
|
|
632
|
-
})
|
|
633
|
-
.catch((e) => {
|
|
634
|
-
log.error("failed to get prompt from MCP server", {
|
|
635
|
-
clientName,
|
|
636
|
-
promptName: name,
|
|
637
|
-
error: e.message,
|
|
638
|
-
})
|
|
639
|
-
return undefined
|
|
640
|
-
})
|
|
641
|
-
|
|
642
|
-
return result
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
export async function readResource(clientName: string, resourceUri: string) {
|
|
646
|
-
const clientsSnapshot = await clients()
|
|
647
|
-
const client = clientsSnapshot[clientName]
|
|
648
|
-
|
|
649
|
-
if (!client) {
|
|
650
|
-
log.warn("client not found for prompt", {
|
|
651
|
-
clientName: clientName,
|
|
652
|
-
})
|
|
653
|
-
return undefined
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
const result = await client
|
|
657
|
-
.readResource({
|
|
658
|
-
uri: resourceUri,
|
|
659
|
-
})
|
|
660
|
-
.catch((e) => {
|
|
661
|
-
log.error("failed to get prompt from MCP server", {
|
|
662
|
-
clientName: clientName,
|
|
663
|
-
resourceUri: resourceUri,
|
|
664
|
-
error: e.message,
|
|
665
|
-
})
|
|
666
|
-
return undefined
|
|
667
|
-
})
|
|
668
|
-
|
|
669
|
-
return result
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* Start OAuth authentication flow for an MCP server.
|
|
674
|
-
* Returns the authorization URL that should be opened in a browser.
|
|
675
|
-
*/
|
|
676
|
-
export async function startAuth(mcpName: string): Promise<{ authorizationUrl: string }> {
|
|
677
|
-
const cfg = await Config.get()
|
|
678
|
-
const mcpConfig = cfg.mcp?.[mcpName]
|
|
679
|
-
|
|
680
|
-
if (!mcpConfig) {
|
|
681
|
-
throw new Error(`MCP server not found: ${mcpName}`)
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
if (!isMcpConfigured(mcpConfig)) {
|
|
685
|
-
throw new Error(`MCP server ${mcpName} is disabled or missing configuration`)
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
if (mcpConfig.type !== "remote") {
|
|
689
|
-
throw new Error(`MCP server ${mcpName} is not a remote server`)
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
if (mcpConfig.oauth === false) {
|
|
693
|
-
throw new Error(`MCP server ${mcpName} has OAuth explicitly disabled`)
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
// Start the callback server
|
|
697
|
-
await McpOAuthCallback.ensureRunning()
|
|
698
|
-
|
|
699
|
-
// Generate and store a cryptographically secure state parameter BEFORE creating the provider
|
|
700
|
-
// The SDK will call provider.state() to read this value
|
|
701
|
-
const oauthState = Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
|
702
|
-
.map((b) => b.toString(16).padStart(2, "0"))
|
|
703
|
-
.join("")
|
|
704
|
-
await McpAuth.updateOAuthState(mcpName, oauthState)
|
|
705
|
-
|
|
706
|
-
// Create a new auth provider for this flow
|
|
707
|
-
// OAuth config is optional - if not provided, we'll use auto-discovery
|
|
708
|
-
const oauthConfig = typeof mcpConfig.oauth === "object" ? mcpConfig.oauth : undefined
|
|
709
|
-
let capturedUrl: URL | undefined
|
|
710
|
-
const authProvider = new McpOAuthProvider(
|
|
711
|
-
mcpName,
|
|
712
|
-
mcpConfig.url,
|
|
713
|
-
{
|
|
714
|
-
clientId: oauthConfig?.clientId,
|
|
715
|
-
clientSecret: oauthConfig?.clientSecret,
|
|
716
|
-
scope: oauthConfig?.scope,
|
|
717
|
-
},
|
|
718
|
-
{
|
|
719
|
-
onRedirect: async (url) => {
|
|
720
|
-
capturedUrl = url
|
|
721
|
-
},
|
|
722
|
-
},
|
|
723
|
-
)
|
|
724
|
-
|
|
725
|
-
// Create transport with auth provider
|
|
726
|
-
const transport = new StreamableHTTPClientTransport(new URL(mcpConfig.url), {
|
|
727
|
-
authProvider,
|
|
728
|
-
})
|
|
729
|
-
|
|
730
|
-
// Try to connect - this will trigger the OAuth flow
|
|
731
|
-
try {
|
|
732
|
-
const client = new Client({
|
|
733
|
-
name: "opencode",
|
|
734
|
-
version: Installation.VERSION,
|
|
735
|
-
})
|
|
736
|
-
await client.connect(transport)
|
|
737
|
-
// If we get here, we're already authenticated
|
|
738
|
-
return { authorizationUrl: "" }
|
|
739
|
-
} catch (error) {
|
|
740
|
-
if (error instanceof UnauthorizedError && capturedUrl) {
|
|
741
|
-
// Store transport for finishAuth
|
|
742
|
-
pendingOAuthTransports.set(mcpName, transport)
|
|
743
|
-
return { authorizationUrl: capturedUrl.toString() }
|
|
744
|
-
}
|
|
745
|
-
throw error
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
/**
|
|
750
|
-
* Complete OAuth authentication after user authorizes in browser.
|
|
751
|
-
* Opens the browser and waits for callback.
|
|
752
|
-
*/
|
|
753
|
-
export async function authenticate(mcpName: string): Promise<Status> {
|
|
754
|
-
const { authorizationUrl } = await startAuth(mcpName)
|
|
755
|
-
|
|
756
|
-
if (!authorizationUrl) {
|
|
757
|
-
// Already authenticated
|
|
758
|
-
const s = await state()
|
|
759
|
-
return s.status[mcpName] ?? { status: "connected" }
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
// Get the state that was already generated and stored in startAuth()
|
|
763
|
-
const oauthState = await McpAuth.getOAuthState(mcpName)
|
|
764
|
-
if (!oauthState) {
|
|
765
|
-
throw new Error("OAuth state not found - this should not happen")
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
// The SDK has already added the state parameter to the authorization URL
|
|
769
|
-
// We just need to open the browser
|
|
770
|
-
log.info("opening browser for oauth", { mcpName, url: authorizationUrl, state: oauthState })
|
|
771
|
-
await open(authorizationUrl)
|
|
772
|
-
|
|
773
|
-
// Wait for callback using the OAuth state parameter
|
|
774
|
-
const code = await McpOAuthCallback.waitForCallback(oauthState)
|
|
775
|
-
|
|
776
|
-
// Validate and clear the state
|
|
777
|
-
const storedState = await McpAuth.getOAuthState(mcpName)
|
|
778
|
-
if (storedState !== oauthState) {
|
|
779
|
-
await McpAuth.clearOAuthState(mcpName)
|
|
780
|
-
throw new Error("OAuth state mismatch - potential CSRF attack")
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
await McpAuth.clearOAuthState(mcpName)
|
|
784
|
-
|
|
785
|
-
// Finish auth
|
|
786
|
-
return finishAuth(mcpName, code)
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
/**
|
|
790
|
-
* Complete OAuth authentication with the authorization code.
|
|
791
|
-
*/
|
|
792
|
-
export async function finishAuth(mcpName: string, authorizationCode: string): Promise<Status> {
|
|
793
|
-
const transport = pendingOAuthTransports.get(mcpName)
|
|
794
|
-
|
|
795
|
-
if (!transport) {
|
|
796
|
-
throw new Error(`No pending OAuth flow for MCP server: ${mcpName}`)
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
try {
|
|
800
|
-
// Call finishAuth on the transport
|
|
801
|
-
await transport.finishAuth(authorizationCode)
|
|
802
|
-
|
|
803
|
-
// Clear the code verifier after successful auth
|
|
804
|
-
await McpAuth.clearCodeVerifier(mcpName)
|
|
805
|
-
|
|
806
|
-
// Now try to reconnect
|
|
807
|
-
const cfg = await Config.get()
|
|
808
|
-
const mcpConfig = cfg.mcp?.[mcpName]
|
|
809
|
-
|
|
810
|
-
if (!mcpConfig) {
|
|
811
|
-
throw new Error(`MCP server not found: ${mcpName}`)
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
if (!isMcpConfigured(mcpConfig)) {
|
|
815
|
-
throw new Error(`MCP server ${mcpName} is disabled or missing configuration`)
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
// Re-add the MCP server to establish connection
|
|
819
|
-
pendingOAuthTransports.delete(mcpName)
|
|
820
|
-
const result = await add(mcpName, mcpConfig)
|
|
821
|
-
|
|
822
|
-
const statusRecord = result.status as Record<string, Status>
|
|
823
|
-
return statusRecord[mcpName] ?? { status: "failed", error: "Unknown error after auth" }
|
|
824
|
-
} catch (error) {
|
|
825
|
-
log.error("failed to finish oauth", { mcpName, error })
|
|
826
|
-
return {
|
|
827
|
-
status: "failed",
|
|
828
|
-
error: error instanceof Error ? error.message : String(error),
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
/**
|
|
834
|
-
* Remove OAuth credentials for an MCP server.
|
|
835
|
-
*/
|
|
836
|
-
export async function removeAuth(mcpName: string): Promise<void> {
|
|
837
|
-
await McpAuth.remove(mcpName)
|
|
838
|
-
McpOAuthCallback.cancelPending(mcpName)
|
|
839
|
-
pendingOAuthTransports.delete(mcpName)
|
|
840
|
-
await McpAuth.clearOAuthState(mcpName)
|
|
841
|
-
log.info("removed oauth credentials", { mcpName })
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
/**
|
|
845
|
-
* Check if an MCP server supports OAuth (remote servers support OAuth by default unless explicitly disabled).
|
|
846
|
-
*/
|
|
847
|
-
export async function supportsOAuth(mcpName: string): Promise<boolean> {
|
|
848
|
-
const cfg = await Config.get()
|
|
849
|
-
const mcpConfig = cfg.mcp?.[mcpName]
|
|
850
|
-
if (!mcpConfig) return false
|
|
851
|
-
if (!isMcpConfigured(mcpConfig)) return false
|
|
852
|
-
return mcpConfig.type === "remote" && mcpConfig.oauth !== false
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
/**
|
|
856
|
-
* Check if an MCP server has stored OAuth tokens.
|
|
857
|
-
*/
|
|
858
|
-
export async function hasStoredTokens(mcpName: string): Promise<boolean> {
|
|
859
|
-
const entry = await McpAuth.get(mcpName)
|
|
860
|
-
return !!entry?.tokens
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
export type AuthStatus = "authenticated" | "expired" | "not_authenticated"
|
|
864
|
-
|
|
865
|
-
/**
|
|
866
|
-
* Get the authentication status for an MCP server.
|
|
867
|
-
*/
|
|
868
|
-
export async function getAuthStatus(mcpName: string): Promise<AuthStatus> {
|
|
869
|
-
const hasTokens = await hasStoredTokens(mcpName)
|
|
870
|
-
if (!hasTokens) return "not_authenticated"
|
|
871
|
-
const expired = await McpAuth.isTokenExpired(mcpName)
|
|
872
|
-
return expired ? "expired" : "authenticated"
|
|
873
|
-
}
|
|
874
|
-
}
|