opencode-acp-provider 1.1.0 → 1.1.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 +56 -23
- package/package.json +3 -2
- package/src/acp-client.ts +67 -4
- package/src/acp-tools/antigravity.ts +117 -0
- package/src/acp-tools/claude.ts +105 -0
- package/src/acp-tools/codex.ts +204 -0
- package/src/acp-tools/command-code.ts +135 -0
- package/src/acp-tools/fallback.ts +92 -0
- package/src/acp-tools/shared.ts +436 -0
- package/src/acp-tools.ts +119 -241
- package/src/agent-command.ts +1 -1
- package/src/agent-pool.ts +5 -1
- package/src/agents/README.md +4 -2
- package/src/agents/cmd/index.ts +1 -1
- package/src/agents/cmd/mods/no-native-orchestration.ts +33 -11
- package/src/index.ts +139 -47
- package/src/inventory.ts +14 -0
- package/src/language-model.ts +27 -13
- package/src/mcp-bridge.mjs +34 -4
- package/src/mcp-server.ts +87 -61
- package/src/prompt.ts +68 -14
- package/src/server-manager.ts +517 -74
- package/src/shim.ts +75 -35
- package/src/unsolicited.ts +388 -0
- package/src/util.ts +60 -2
- package/src/mcp-config.ts +0 -198
package/src/mcp-config.ts
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import type { Mcp, Plugin } from "@opencode/plugin"
|
|
2
|
-
import type { AcpMcpServer } from "./acp-client.ts"
|
|
3
|
-
import type { AcpServerConfig, LogFn } from "./server-manager.ts"
|
|
4
|
-
import { executableName, isCmdCommand } from "./agent-command.ts"
|
|
5
|
-
import { errorMessage } from "./util.ts"
|
|
6
|
-
|
|
7
|
-
type Ctx = Plugin.Context
|
|
8
|
-
type ServerConfig = Mcp.ServerConfig
|
|
9
|
-
|
|
10
|
-
export interface OpenCodeMcpSource {
|
|
11
|
-
/** Servers to add to an ACP `session/new`, read from the current config. */
|
|
12
|
-
servers: (agent: AcpServerConfig) => Promise<AcpMcpServer[]>
|
|
13
|
-
close: () => Promise<void>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface OauthHint {
|
|
17
|
-
/** How the agent is named in the warning. */
|
|
18
|
-
label: string
|
|
19
|
-
/** Commands that give it a grant; empty when its CLI is not recognized. */
|
|
20
|
-
steps: string[]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* How one agent signs in to a remote MCP server, when its executable is
|
|
25
|
-
* recognizable. Each CLI keys credentials by a server it already has in its own
|
|
26
|
-
* config, so a login command alone fails with "no MCP server named ...": the
|
|
27
|
-
* server has to be registered with the agent first.
|
|
28
|
-
*
|
|
29
|
-
* Syntax checked against `cmd`, `codex`, and `claude` (`mcp --help`).
|
|
30
|
-
*/
|
|
31
|
-
function oauthHint(agent: AcpServerConfig, name: string, url: string): OauthHint {
|
|
32
|
-
const server = JSON.stringify(name)
|
|
33
|
-
const target = JSON.stringify(url)
|
|
34
|
-
if (isCmdCommand(agent.command))
|
|
35
|
-
return {
|
|
36
|
-
label: "Command Code",
|
|
37
|
-
steps: [`cmd mcp add --transport http ${server} ${target}`, `cmd mcp auth ${server}`],
|
|
38
|
-
}
|
|
39
|
-
const executable = executableName(agent.command)
|
|
40
|
-
if (executable.startsWith("codex"))
|
|
41
|
-
return {
|
|
42
|
-
label: "Codex",
|
|
43
|
-
steps: [`codex mcp add ${server} --url ${target}`, `codex mcp login ${server}`],
|
|
44
|
-
}
|
|
45
|
-
if (executable.startsWith("claude"))
|
|
46
|
-
return {
|
|
47
|
-
label: "Claude Code",
|
|
48
|
-
steps: [`claude mcp add --transport http ${server} ${target}`, `claude mcp login ${server}`],
|
|
49
|
-
}
|
|
50
|
-
return { label: executable || "the ACP agent", steps: [] }
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** How OpenCode records a remote server's sign-in, as reported by `mcp.list()`. */
|
|
54
|
-
interface McpAuth {
|
|
55
|
-
/** Set while OpenCode has a credential for this server. */
|
|
56
|
-
integrationID?: string
|
|
57
|
-
/** OpenCode says the server is reachable but needs the user to sign in. */
|
|
58
|
-
needsAuth: boolean
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* OpenCode's own MCP servers, converted to the shape ACP accepts, so an agent
|
|
63
|
-
* connects to exactly the servers OpenCode is configured with instead of a
|
|
64
|
-
* second list maintained next to it.
|
|
65
|
-
*
|
|
66
|
-
* Only an MCP transform exposes the full configuration; `ctx.mcp.list()` reports
|
|
67
|
-
* connection status and the integration holding a remote server's sign-in. The
|
|
68
|
-
* transform only reads, and it is replayed whenever the registry is rebuilt,
|
|
69
|
-
* which keeps the capture current for later sessions.
|
|
70
|
-
*/
|
|
71
|
-
export async function createOpenCodeMcpSource(ctx: Ctx, log: LogFn): Promise<OpenCodeMcpSource> {
|
|
72
|
-
let configs: ReadonlyArray<readonly [string, ServerConfig]> = []
|
|
73
|
-
const registration = await ctx.mcp.transform((editor) => {
|
|
74
|
-
configs = editor.list()
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
let auth = new Map<string, McpAuth>()
|
|
78
|
-
const warned = new Set<string>()
|
|
79
|
-
const warnOnce = (key: string, message: string) => {
|
|
80
|
-
if (warned.has(key)) return
|
|
81
|
-
warned.add(key)
|
|
82
|
-
log("warn", message)
|
|
83
|
-
}
|
|
84
|
-
let reading: Promise<void> | undefined
|
|
85
|
-
/** Read each server's runtime status and sign-in integration from OpenCode. */
|
|
86
|
-
const readAuth = () => {
|
|
87
|
-
if (reading) return reading
|
|
88
|
-
reading = (async () => {
|
|
89
|
-
try {
|
|
90
|
-
const listing = await ctx.mcp.list()
|
|
91
|
-
auth = new Map(
|
|
92
|
-
listing.data.map((server) => [
|
|
93
|
-
server.name,
|
|
94
|
-
{
|
|
95
|
-
...(server.integrationID ? { integrationID: server.integrationID } : {}),
|
|
96
|
-
needsAuth: server.status.status === "needs_auth",
|
|
97
|
-
},
|
|
98
|
-
]),
|
|
99
|
-
)
|
|
100
|
-
} catch (error) {
|
|
101
|
-
warnOnce("mcp:auth", `could not read OpenCode's MCP authentication: ${errorMessage(error)}`)
|
|
102
|
-
}
|
|
103
|
-
})().finally(() => {
|
|
104
|
-
reading = undefined
|
|
105
|
-
})
|
|
106
|
-
return reading
|
|
107
|
-
}
|
|
108
|
-
// A registry read replays the registered transforms, so this also captures the
|
|
109
|
-
// configuration for plugin setups that read nothing else.
|
|
110
|
-
await readAuth()
|
|
111
|
-
|
|
112
|
-
const warnSignedIn = (agent: AcpServerConfig, name: string, url: string) => {
|
|
113
|
-
const hint = oauthHint(agent, name, url)
|
|
114
|
-
const own = hint.steps.length ? ` Or run ${hint.steps.join(" then ")} to sign in ${hint.label} separately.` : ""
|
|
115
|
-
// Keyed by agent: each one needs its own grant when OpenCode's cannot be
|
|
116
|
-
// forwarded, and a warning logged for one says nothing about another.
|
|
117
|
-
warnOnce(
|
|
118
|
-
`${agent.id}:${name}:oauth`,
|
|
119
|
-
`MCP server "${name}" has no OpenCode OAuth sign-in to forward; ${hint.label} cannot use it until it is authenticated. Run opencode mcp auth ${JSON.stringify(name)} to sign in through OpenCode.${own}`,
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** A static ACP header carrying OpenCode's active OAuth grant for a server. */
|
|
124
|
-
const signInHeader = async (integrationID: string) => {
|
|
125
|
-
const connection = await ctx.integration.connection.active(integrationID)
|
|
126
|
-
if (!connection) return undefined
|
|
127
|
-
const credential = await ctx.integration.connection.resolve(connection)
|
|
128
|
-
if (!credential || credential.type !== "oauth" || !credential.access) return undefined
|
|
129
|
-
return `Bearer ${credential.access}`
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
async servers(agent) {
|
|
134
|
-
// Read on every session open, not just at setup: a sign-in can arrive
|
|
135
|
-
// later, and a credential can be refreshed between sessions.
|
|
136
|
-
if (configs.some(([, config]) => config.type === "remote" && !config.disabled)) await readAuth()
|
|
137
|
-
|
|
138
|
-
const servers: AcpMcpServer[] = []
|
|
139
|
-
for (const [name, config] of configs) {
|
|
140
|
-
if (config.disabled) continue
|
|
141
|
-
if (config.type === "local") {
|
|
142
|
-
const [command, ...args] = config.command ?? []
|
|
143
|
-
if (!command) {
|
|
144
|
-
warnOnce(`${name}:command`, `MCP server "${name}" has no command; skipping it for ACP agents`)
|
|
145
|
-
continue
|
|
146
|
-
}
|
|
147
|
-
if (config.cwd) {
|
|
148
|
-
warnOnce(
|
|
149
|
-
`${name}:cwd`,
|
|
150
|
-
`MCP server "${name}" sets cwd, which ACP cannot forward; the agent starts it in its own directory`,
|
|
151
|
-
)
|
|
152
|
-
}
|
|
153
|
-
const env = pairs(config.environment)
|
|
154
|
-
// Codex ACP's stdio variant requires the ACP transport tag. Other ACP
|
|
155
|
-
// agents accept the standard untagged v1 form too, but the tagged
|
|
156
|
-
// shape survives v1/v2 adapters that normalize by transport.
|
|
157
|
-
servers.push({ type: "stdio", name, command, args, ...(env.length ? { env } : {}) })
|
|
158
|
-
continue
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const headers = pairs(config.headers)
|
|
162
|
-
const entry = auth.get(name)
|
|
163
|
-
const wantsOAuth = config.oauth !== false
|
|
164
|
-
let signedIn = hasHeader(headers, "authorization")
|
|
165
|
-
if (entry?.integrationID && !signedIn && wantsOAuth) {
|
|
166
|
-
try {
|
|
167
|
-
const header = await signInHeader(entry.integrationID)
|
|
168
|
-
if (header) {
|
|
169
|
-
headers.push({ name: "Authorization", value: header })
|
|
170
|
-
signedIn = true
|
|
171
|
-
}
|
|
172
|
-
} catch (error) {
|
|
173
|
-
warnOnce(
|
|
174
|
-
`${agent.id}:${name}:credential`,
|
|
175
|
-
`could not read the OpenCode sign-in for MCP server "${name}": ${errorMessage(error)}`,
|
|
176
|
-
)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
// Config headers can carry a key the user set up explicitly; only fall
|
|
180
|
-
// back to a warning when nothing at all authenticates the server.
|
|
181
|
-
if (!signedIn && wantsOAuth && (config.oauth || entry?.integrationID || entry?.needsAuth)) {
|
|
182
|
-
warnSignedIn(agent, name, config.url)
|
|
183
|
-
}
|
|
184
|
-
servers.push({ type: "http", name, url: config.url, headers })
|
|
185
|
-
}
|
|
186
|
-
return servers
|
|
187
|
-
},
|
|
188
|
-
close: () => registration.dispose(),
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function hasHeader(headers: Array<{ name: string; value: string }>, name: string) {
|
|
193
|
-
return headers.some((header) => header.name.toLowerCase() === name)
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function pairs(input: Record<string, string> | undefined) {
|
|
197
|
-
return Object.entries(input ?? {}).map(([name, value]) => ({ name, value }))
|
|
198
|
-
}
|