clanka 0.10.1 → 0.12.0
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 +75 -0
- package/dist/Acp.d.ts +1 -1
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +112 -7
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +178 -0
- package/dist/Acp.test.js.map +1 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +26 -8
- package/dist/Agent.js.map +1 -1
- package/dist/AgentExecutor.js +15 -1
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +24 -4
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentSkills.test.js +0 -1
- package/dist/AgentSkills.test.js.map +1 -1
- package/dist/AgentSubagents.test.js +16 -6
- package/dist/AgentSubagents.test.js.map +1 -1
- package/dist/AgentTools.d.ts +7 -0
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +7 -0
- package/dist/AgentTools.js.map +1 -1
- package/dist/McpConfig.d.ts +36 -0
- package/dist/McpConfig.d.ts.map +1 -0
- package/dist/McpConfig.js +182 -0
- package/dist/McpConfig.js.map +1 -0
- package/dist/McpConfig.test.d.ts +2 -0
- package/dist/McpConfig.test.d.ts.map +1 -0
- package/dist/McpConfig.test.js +163 -0
- package/dist/McpConfig.test.js.map +1 -0
- package/dist/McpServers.d.ts +176 -0
- package/dist/McpServers.d.ts.map +1 -0
- package/dist/McpServers.js +707 -0
- package/dist/McpServers.js.map +1 -0
- package/dist/McpServers.test.d.ts +2 -0
- package/dist/McpServers.test.d.ts.map +1 -0
- package/dist/McpServers.test.js +717 -0
- package/dist/McpServers.test.js.map +1 -0
- package/dist/TypeBuilder.d.ts +9 -0
- package/dist/TypeBuilder.d.ts.map +1 -1
- package/dist/TypeBuilder.js +24 -11
- package/dist/TypeBuilder.js.map +1 -1
- package/dist/TypeBuilder.test.js +33 -0
- package/dist/TypeBuilder.test.js.map +1 -1
- package/dist/bin/{Effect-2jPlJ8eu.mjs → Effect-DuThyCJ1.mjs} +155 -74
- package/dist/bin/Effect-DuThyCJ1.mjs.map +1 -0
- package/dist/bin/McpClient-CIiPyMDJ.mjs +36 -0
- package/dist/bin/McpClient-CIiPyMDJ.mjs.map +1 -0
- package/dist/bin/{OutputFormatter-BFAv48k9.mjs → OutputFormatter-BvIjvUmT.mjs} +3 -3
- package/dist/bin/{OutputFormatter-BFAv48k9.mjs.map → OutputFormatter-BvIjvUmT.mjs.map} +1 -1
- package/dist/bin/{Schema-D-7VuCiC.mjs → Schema-DcXUXpRg.mjs} +1069 -124
- package/dist/bin/Schema-DcXUXpRg.mjs.map +1 -0
- package/dist/bin/{SemanticSearch-Cb8aqvGv.mjs → SemanticSearch-CdGA4CIC.mjs} +27 -21
- package/dist/bin/SemanticSearch-CdGA4CIC.mjs.map +1 -0
- package/dist/bin/{Stream-DEu2tfCV.mjs → Stream-I9SRMlLj.mjs} +60 -45
- package/dist/bin/Stream-I9SRMlLj.mjs.map +1 -0
- package/dist/bin/cli.mjs +27217 -10325
- package/dist/bin/cli.mjs.map +1 -1
- package/dist/bin/{index.min-CI3qqohw.mjs → index.min-Bkz7vIWv.mjs} +7 -7
- package/dist/bin/index.min-Bkz7vIWv.mjs.map +1 -0
- package/dist/bin/rolldown-runtime-zY4HT0JA.mjs +30 -0
- package/dist/bin/{turndown.cjs-DK29wZw3.mjs → turndown.cjs-LntFxFyd.mjs} +2 -2
- package/dist/bin/{turndown.cjs-DK29wZw3.mjs.map → turndown.cjs-LntFxFyd.mjs.map} +1 -1
- package/dist/cli.js +46 -7
- package/dist/cli.js.map +1 -1
- package/dist/fixtures/McpFakeServer.d.ts +47 -0
- package/dist/fixtures/McpFakeServer.d.ts.map +1 -0
- package/dist/fixtures/McpFakeServer.js +112 -0
- package/dist/fixtures/McpFakeServer.js.map +1 -0
- package/dist/fixtures/McpStdioServer.d.ts +2 -0
- package/dist/fixtures/McpStdioServer.d.ts.map +1 -0
- package/dist/fixtures/McpStdioServer.js +53 -0
- package/dist/fixtures/McpStdioServer.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/package.json +14 -14
- package/src/Acp.test.ts +242 -0
- package/src/Acp.ts +141 -5
- package/src/Agent.ts +38 -10
- package/src/AgentExecutor.test.ts +31 -5
- package/src/AgentExecutor.ts +14 -1
- package/src/AgentSkills.test.ts +0 -1
- package/src/AgentSubagents.test.ts +20 -6
- package/src/AgentTools.ts +8 -0
- package/src/McpConfig.test.ts +238 -0
- package/src/McpConfig.ts +255 -0
- package/src/McpServers.test.ts +1050 -0
- package/src/McpServers.ts +1072 -0
- package/src/TypeBuilder.test.ts +70 -0
- package/src/TypeBuilder.ts +41 -14
- package/src/cli.ts +92 -8
- package/src/fixtures/McpFakeServer.ts +162 -0
- package/src/fixtures/McpStdioServer.ts +60 -0
- package/src/index.ts +5 -0
- package/dist/bin/Effect-2jPlJ8eu.mjs.map +0 -1
- package/dist/bin/McpClient-1W508adc.mjs +0 -14179
- package/dist/bin/McpClient-1W508adc.mjs.map +0 -1
- package/dist/bin/Schema-D-7VuCiC.mjs.map +0 -1
- package/dist/bin/SemanticSearch-Cb8aqvGv.mjs.map +0 -1
- package/dist/bin/Stream-DEu2tfCV.mjs.map +0 -1
- package/dist/bin/index.min-CI3qqohw.mjs.map +0 -1
package/src/Acp.test.ts
CHANGED
|
@@ -6,6 +6,8 @@ import * as FileSystem from "effect/FileSystem"
|
|
|
6
6
|
import * as Fiber from "effect/Fiber"
|
|
7
7
|
import * as Layer from "effect/Layer"
|
|
8
8
|
import * as Option from "effect/Option"
|
|
9
|
+
import * as Path from "effect/Path"
|
|
10
|
+
import * as Schedule from "effect/Schedule"
|
|
9
11
|
import * as Stream from "effect/Stream"
|
|
10
12
|
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
11
13
|
import * as Prompt from "effect/ai/Prompt"
|
|
@@ -17,6 +19,7 @@ import * as Acp from "./Acp.ts"
|
|
|
17
19
|
import * as Agent from "./Agent.ts"
|
|
18
20
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
19
21
|
import * as Compaction from "./Compaction.ts"
|
|
22
|
+
import { listenHttp, makeFakeServer, text } from "./fixtures/McpFakeServer.ts"
|
|
20
23
|
|
|
21
24
|
type Message = {
|
|
22
25
|
readonly id?: number
|
|
@@ -950,3 +953,242 @@ await changeDirectory("sub")`),
|
|
|
950
953
|
),
|
|
951
954
|
)
|
|
952
955
|
})
|
|
956
|
+
|
|
957
|
+
// --- MCP servers ---------------------------------------------------------------
|
|
958
|
+
|
|
959
|
+
const systemText = (prompt: Prompt.Prompt): string =>
|
|
960
|
+
prompt.content
|
|
961
|
+
.filter((message) => message.role === "system")
|
|
962
|
+
.map((message) => message.content)
|
|
963
|
+
.join("\n")
|
|
964
|
+
|
|
965
|
+
const isRunning = (pid: number): boolean => {
|
|
966
|
+
try {
|
|
967
|
+
process.kill(pid, 0)
|
|
968
|
+
return true
|
|
969
|
+
} catch {
|
|
970
|
+
return false
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
const exited = (pid: number) =>
|
|
975
|
+
Effect.sync(() => isRunning(pid)).pipe(
|
|
976
|
+
Effect.repeat({
|
|
977
|
+
while: (running) => running,
|
|
978
|
+
schedule: Schedule.spaced("50 millis"),
|
|
979
|
+
}),
|
|
980
|
+
Effect.timeout("3 seconds"),
|
|
981
|
+
)
|
|
982
|
+
|
|
983
|
+
const remoteServer = (path?: string) =>
|
|
984
|
+
Effect.acquireRelease(
|
|
985
|
+
Effect.promise(() =>
|
|
986
|
+
listenHttp(
|
|
987
|
+
makeFakeServer({
|
|
988
|
+
tools: [{ name: "ping", call: () => text("pong") }],
|
|
989
|
+
}),
|
|
990
|
+
path,
|
|
991
|
+
),
|
|
992
|
+
),
|
|
993
|
+
(remote) => Effect.promise(remote.close),
|
|
994
|
+
)
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* An ACP stdio entry running the fixture server, which writes its pid and cwd
|
|
998
|
+
* to `out`.
|
|
999
|
+
*/
|
|
1000
|
+
const stdioServer = (path: Path.Path, name: string, out: string) => ({
|
|
1001
|
+
name,
|
|
1002
|
+
command: process.execPath,
|
|
1003
|
+
args: [path.join(import.meta.dirname, "fixtures", "McpStdioServer.ts")],
|
|
1004
|
+
env: [{ name: "FIXTURE_OUT", value: out }],
|
|
1005
|
+
})
|
|
1006
|
+
|
|
1007
|
+
const readStarted = (out: string) =>
|
|
1008
|
+
Effect.gen(function* () {
|
|
1009
|
+
const fs = yield* FileSystem.FileSystem
|
|
1010
|
+
return JSON.parse(yield* fs.readFileString(out)) as {
|
|
1011
|
+
readonly pid: number
|
|
1012
|
+
readonly cwd: string
|
|
1013
|
+
}
|
|
1014
|
+
})
|
|
1015
|
+
|
|
1016
|
+
describe("Acp MCP servers", () => {
|
|
1017
|
+
it.live("connects stdio and http servers and reports failed ones once", () =>
|
|
1018
|
+
withStore(
|
|
1019
|
+
Effect.gen(function* () {
|
|
1020
|
+
const fs = yield* FileSystem.FileSystem
|
|
1021
|
+
const path = yield* Path.Path
|
|
1022
|
+
const cwd = yield* fs.makeTempDirectoryScoped()
|
|
1023
|
+
const out = path.join(cwd, "started.json")
|
|
1024
|
+
const remote = yield* remoteServer()
|
|
1025
|
+
const systems: Array<string> = []
|
|
1026
|
+
const server = yield* makeServer((options) => {
|
|
1027
|
+
systems.push(systemText(options.prompt))
|
|
1028
|
+
return assistantSays("ok")
|
|
1029
|
+
})
|
|
1030
|
+
|
|
1031
|
+
const init = yield* server.request(1, "initialize", {
|
|
1032
|
+
protocolVersion: 1,
|
|
1033
|
+
})
|
|
1034
|
+
assert.deepStrictEqual(init.result.agentCapabilities.mcpCapabilities, {
|
|
1035
|
+
http: true,
|
|
1036
|
+
sse: false,
|
|
1037
|
+
})
|
|
1038
|
+
assert.isDefined(
|
|
1039
|
+
init.result.agentCapabilities.sessionCapabilities?.close,
|
|
1040
|
+
)
|
|
1041
|
+
|
|
1042
|
+
const created = yield* server.request(2, "session/new", {
|
|
1043
|
+
cwd,
|
|
1044
|
+
mcpServers: [
|
|
1045
|
+
// stdio has no `type`.
|
|
1046
|
+
stdioServer(path, "acpstdio", out),
|
|
1047
|
+
{
|
|
1048
|
+
type: "http",
|
|
1049
|
+
name: "acphttp",
|
|
1050
|
+
url: remote.url,
|
|
1051
|
+
headers: [{ name: "Authorization", value: "Bearer secret" }],
|
|
1052
|
+
},
|
|
1053
|
+
{
|
|
1054
|
+
type: "sse",
|
|
1055
|
+
name: "acplegacy",
|
|
1056
|
+
url: "http://127.0.0.1:9/sse",
|
|
1057
|
+
headers: [],
|
|
1058
|
+
},
|
|
1059
|
+
// A later duplicate is skipped.
|
|
1060
|
+
{
|
|
1061
|
+
name: "acpstdio",
|
|
1062
|
+
command: "/nonexistent/clanka-mcp",
|
|
1063
|
+
args: [],
|
|
1064
|
+
env: [],
|
|
1065
|
+
},
|
|
1066
|
+
],
|
|
1067
|
+
})
|
|
1068
|
+
assert.isUndefined(created.error)
|
|
1069
|
+
const sessionId: string = created.result.sessionId
|
|
1070
|
+
for (const id of [3, 4]) {
|
|
1071
|
+
yield* server.request(id, "session/prompt", {
|
|
1072
|
+
sessionId,
|
|
1073
|
+
prompt: [{ type: "text", text: "hello" }],
|
|
1074
|
+
})
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
const failures = server
|
|
1078
|
+
.updates(sessionId)
|
|
1079
|
+
.filter(
|
|
1080
|
+
(u) =>
|
|
1081
|
+
u.sessionUpdate === "agent_thought_chunk" &&
|
|
1082
|
+
String(u.content.text).includes("MCP server"),
|
|
1083
|
+
)
|
|
1084
|
+
.map((u) => String(u.content.text))
|
|
1085
|
+
assert.strictEqual(failures.length, 1)
|
|
1086
|
+
assert.include(
|
|
1087
|
+
failures[0],
|
|
1088
|
+
'MCP server "acplegacy" failed: SSE transport is not supported',
|
|
1089
|
+
)
|
|
1090
|
+
assert.notInclude(failures[0], "acpstdio")
|
|
1091
|
+
|
|
1092
|
+
assert.include(systems[0], "acpstdio")
|
|
1093
|
+
assert.include(systems[0], "acphttp")
|
|
1094
|
+
// Stdio servers run in the session's cwd.
|
|
1095
|
+
const started = yield* readStarted(out)
|
|
1096
|
+
assert.strictEqual(started.cwd, yield* fs.realPath(cwd))
|
|
1097
|
+
assert.isNotEmpty(remote.authorization)
|
|
1098
|
+
for (const header of remote.authorization) {
|
|
1099
|
+
assert.strictEqual(header, "Bearer secret")
|
|
1100
|
+
}
|
|
1101
|
+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
|
1102
|
+
),
|
|
1103
|
+
)
|
|
1104
|
+
|
|
1105
|
+
it.live(
|
|
1106
|
+
"resume connects the sent servers, which are never persisted, and close ends them",
|
|
1107
|
+
() =>
|
|
1108
|
+
withStore(
|
|
1109
|
+
Effect.gen(function* () {
|
|
1110
|
+
const fs = yield* FileSystem.FileSystem
|
|
1111
|
+
const path = yield* Path.Path
|
|
1112
|
+
const kvs = yield* KeyValueStore.KeyValueStore
|
|
1113
|
+
const cwd = yield* fs.makeTempDirectoryScoped()
|
|
1114
|
+
const remote = yield* remoteServer("/BROKER-TOKEN")
|
|
1115
|
+
const systems: Array<string> = []
|
|
1116
|
+
const server = yield* makeServer((options) => {
|
|
1117
|
+
systems.push(systemText(options.prompt))
|
|
1118
|
+
return assistantSays("ok")
|
|
1119
|
+
})
|
|
1120
|
+
const prompt = (id: number, sessionId: string) =>
|
|
1121
|
+
server.request(id, "session/prompt", {
|
|
1122
|
+
sessionId,
|
|
1123
|
+
prompt: [{ type: "text", text: "hello" }],
|
|
1124
|
+
})
|
|
1125
|
+
|
|
1126
|
+
const created = yield* server.request(1, "session/new", {
|
|
1127
|
+
cwd,
|
|
1128
|
+
mcpServers: [
|
|
1129
|
+
stdioServer(path, "acpfirst", path.join(cwd, "first.json")),
|
|
1130
|
+
{ type: "http", name: "acpbroker", url: remote.url, headers: [] },
|
|
1131
|
+
],
|
|
1132
|
+
})
|
|
1133
|
+
const sessionId: string = created.result.sessionId
|
|
1134
|
+
yield* prompt(2, sessionId)
|
|
1135
|
+
const first = yield* readStarted(path.join(cwd, "first.json"))
|
|
1136
|
+
|
|
1137
|
+
const closed = yield* server.request(3, "session/close", {
|
|
1138
|
+
sessionId,
|
|
1139
|
+
})
|
|
1140
|
+
assert.isUndefined(closed.error)
|
|
1141
|
+
yield* exited(first.pid)
|
|
1142
|
+
const afterClose = yield* prompt(4, sessionId)
|
|
1143
|
+
assert.isDefined(afterClose.error)
|
|
1144
|
+
|
|
1145
|
+
const persisted = yield* kvs.get(`session-${sessionId}`)
|
|
1146
|
+
assert.isDefined(persisted)
|
|
1147
|
+
assert.notInclude(persisted, "BROKER-TOKEN")
|
|
1148
|
+
|
|
1149
|
+
const resumed = yield* server.request(5, "session/resume", {
|
|
1150
|
+
sessionId,
|
|
1151
|
+
cwd,
|
|
1152
|
+
mcpServers: [
|
|
1153
|
+
stdioServer(path, "acpsecond", path.join(cwd, "second.json")),
|
|
1154
|
+
],
|
|
1155
|
+
})
|
|
1156
|
+
assert.isUndefined(resumed.error)
|
|
1157
|
+
yield* prompt(6, sessionId)
|
|
1158
|
+
const system = systems.at(-1)!
|
|
1159
|
+
assert.include(system, "acpsecond")
|
|
1160
|
+
assert.notInclude(system, "acpfirst")
|
|
1161
|
+
assert.notInclude(system, "acpbroker")
|
|
1162
|
+
|
|
1163
|
+
const second = yield* readStarted(path.join(cwd, "second.json"))
|
|
1164
|
+
yield* server.request(7, "session/close", { sessionId })
|
|
1165
|
+
yield* exited(second.pid)
|
|
1166
|
+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
|
1167
|
+
),
|
|
1168
|
+
)
|
|
1169
|
+
|
|
1170
|
+
it.live("starts servers named like Object.prototype members", () =>
|
|
1171
|
+
withStore(
|
|
1172
|
+
Effect.gen(function* () {
|
|
1173
|
+
const fs = yield* FileSystem.FileSystem
|
|
1174
|
+
const path = yield* Path.Path
|
|
1175
|
+
const cwd = yield* fs.makeTempDirectoryScoped()
|
|
1176
|
+
const server = yield* makeServer(() => assistantSays("ok"))
|
|
1177
|
+
const names = ["constructor", "__proto__"]
|
|
1178
|
+
const created = yield* server.request(1, "session/new", {
|
|
1179
|
+
cwd,
|
|
1180
|
+
mcpServers: names.map((name) =>
|
|
1181
|
+
stdioServer(path, name, path.join(cwd, `${name}.json`)),
|
|
1182
|
+
),
|
|
1183
|
+
})
|
|
1184
|
+
assert.isUndefined(created.error)
|
|
1185
|
+
for (const name of names) {
|
|
1186
|
+
assert.isTrue(
|
|
1187
|
+
yield* fs.exists(path.join(cwd, `${name}.json`)),
|
|
1188
|
+
`${name} was not started`,
|
|
1189
|
+
)
|
|
1190
|
+
}
|
|
1191
|
+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
|
1192
|
+
),
|
|
1193
|
+
)
|
|
1194
|
+
})
|
package/src/Acp.ts
CHANGED
|
@@ -9,17 +9,19 @@
|
|
|
9
9
|
* @since 1.0.0
|
|
10
10
|
*/
|
|
11
11
|
import * as Cause from "effect/Cause"
|
|
12
|
+
import * as Context from "effect/Context"
|
|
12
13
|
import * as Effect from "effect/Effect"
|
|
13
14
|
import * as Base64 from "effect/encoding/Base64"
|
|
14
15
|
import * as Exit from "effect/Exit"
|
|
15
16
|
import * as Fiber from "effect/Fiber"
|
|
16
17
|
import * as FileSystem from "effect/FileSystem"
|
|
17
|
-
import
|
|
18
|
+
import * as Layer from "effect/Layer"
|
|
18
19
|
import * as MutableRef from "effect/MutableRef"
|
|
19
20
|
import * as Option from "effect/Option"
|
|
20
21
|
import * as Path from "effect/Path"
|
|
21
22
|
import type * as PlatformError from "effect/PlatformError"
|
|
22
23
|
import * as Queue from "effect/Queue"
|
|
24
|
+
import * as Redacted from "effect/Redacted"
|
|
23
25
|
import * as Schema from "effect/Schema"
|
|
24
26
|
import * as Scope from "effect/Scope"
|
|
25
27
|
import * as Stdio from "effect/Stdio"
|
|
@@ -34,6 +36,7 @@ import * as AgentExecutor from "./AgentExecutor.ts"
|
|
|
34
36
|
import type * as AgentOutput from "./AgentOutput.ts"
|
|
35
37
|
import * as Compaction from "./Compaction.ts"
|
|
36
38
|
import * as Image from "./Image.ts"
|
|
39
|
+
import * as McpServers from "./McpServers.ts"
|
|
37
40
|
|
|
38
41
|
/**
|
|
39
42
|
* @since 1.0.0
|
|
@@ -153,15 +156,91 @@ const decodeIncoming = Schema.decodeEffect(
|
|
|
153
156
|
Schema.fromJsonString(IncomingMessage),
|
|
154
157
|
)
|
|
155
158
|
|
|
159
|
+
const NameValue = Schema.Struct({ name: Schema.String, value: Schema.String })
|
|
160
|
+
|
|
161
|
+
// stdio entries carry no `type`.
|
|
162
|
+
const McpServer = Schema.Union([
|
|
163
|
+
Schema.Struct({
|
|
164
|
+
type: Schema.Literal("http"),
|
|
165
|
+
name: Schema.String,
|
|
166
|
+
url: Schema.String,
|
|
167
|
+
headers: Schema.optionalKey(Schema.Array(NameValue)),
|
|
168
|
+
}),
|
|
169
|
+
Schema.Struct({
|
|
170
|
+
type: Schema.Literal("sse"),
|
|
171
|
+
name: Schema.String,
|
|
172
|
+
}),
|
|
173
|
+
Schema.Struct({
|
|
174
|
+
type: Schema.optionalKey(Schema.Literal("stdio")),
|
|
175
|
+
name: Schema.String,
|
|
176
|
+
command: Schema.String,
|
|
177
|
+
args: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
178
|
+
env: Schema.optionalKey(Schema.Array(NameValue)),
|
|
179
|
+
}),
|
|
180
|
+
])
|
|
181
|
+
|
|
156
182
|
const NewSessionParams = Schema.Struct({
|
|
157
183
|
cwd: Schema.String,
|
|
158
184
|
model: Schema.optionalKey(Schema.String),
|
|
185
|
+
mcpServers: Schema.optionalKey(Schema.Array(McpServer)),
|
|
159
186
|
})
|
|
160
187
|
|
|
161
188
|
const LoadSessionParams = Schema.Struct({
|
|
162
189
|
sessionId: Schema.String,
|
|
163
190
|
cwd: Schema.optionalKey(Schema.String),
|
|
164
191
|
model: Schema.optionalKey(Schema.String),
|
|
192
|
+
mcpServers: Schema.optionalKey(Schema.Array(McpServer)),
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
const redactValues = (
|
|
196
|
+
entries: ReadonlyArray<typeof NameValue.Type> | undefined,
|
|
197
|
+
): Record<string, Redacted.Redacted<string>> =>
|
|
198
|
+
Object.fromEntries(
|
|
199
|
+
(entries ?? []).map(({ name, value }) => [name, Redacted.make(value)]),
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The servers sent with `session/new`, `session/load` or `session/resume`.
|
|
204
|
+
* stdio servers run in the session's cwd; the first of two servers with the
|
|
205
|
+
* same name wins.
|
|
206
|
+
*/
|
|
207
|
+
const mcpServerEntries = Effect.fnUntraced(function* (
|
|
208
|
+
servers: ReadonlyArray<typeof McpServer.Type>,
|
|
209
|
+
cwd: string,
|
|
210
|
+
) {
|
|
211
|
+
const entries: Record<string, McpServers.ServerEntry> = Object.create(null)
|
|
212
|
+
for (const server of servers) {
|
|
213
|
+
if (Object.hasOwn(entries, server.name)) {
|
|
214
|
+
yield* Effect.logWarning("Skipping duplicate MCP server").pipe(
|
|
215
|
+
Effect.annotateLogs({ server: server.name }),
|
|
216
|
+
)
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
switch (server.type) {
|
|
220
|
+
case "http":
|
|
221
|
+
entries[server.name] = {
|
|
222
|
+
type: "http",
|
|
223
|
+
url: server.url,
|
|
224
|
+
headers: redactValues(server.headers),
|
|
225
|
+
}
|
|
226
|
+
break
|
|
227
|
+
case "sse":
|
|
228
|
+
entries[server.name] = {
|
|
229
|
+
type: "unavailable",
|
|
230
|
+
error: "SSE transport is not supported",
|
|
231
|
+
}
|
|
232
|
+
break
|
|
233
|
+
default:
|
|
234
|
+
entries[server.name] = {
|
|
235
|
+
type: "stdio",
|
|
236
|
+
command: server.command,
|
|
237
|
+
args: server.args ?? [],
|
|
238
|
+
env: redactValues(server.env),
|
|
239
|
+
cwd,
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return entries
|
|
165
244
|
})
|
|
166
245
|
|
|
167
246
|
const SessionIdParams = Schema.Struct({
|
|
@@ -304,6 +383,10 @@ interface Session {
|
|
|
304
383
|
running: Fiber.Fiber<void, unknown> | undefined
|
|
305
384
|
toolCalls: number
|
|
306
385
|
readonly announcedSubagents: Set<number>
|
|
386
|
+
/** Owns the Agent and the session's MCP servers. */
|
|
387
|
+
readonly scope: Scope.Closeable
|
|
388
|
+
/** Failed MCP servers, reported on the first prompt turn. */
|
|
389
|
+
mcpFailures: string | undefined
|
|
307
390
|
}
|
|
308
391
|
|
|
309
392
|
/**
|
|
@@ -395,8 +478,31 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
395
478
|
const openSession = Effect.fnUntraced(function* (
|
|
396
479
|
id: string,
|
|
397
480
|
record: SessionRecord,
|
|
481
|
+
mcpServers: ReadonlyArray<typeof McpServer.Type>,
|
|
398
482
|
) {
|
|
399
|
-
const
|
|
483
|
+
const sessionScope = yield* Scope.fork(scope)
|
|
484
|
+
let makeAgent = options.makeAgent(record.cwd)
|
|
485
|
+
let mcpFailures: string | undefined
|
|
486
|
+
// Never stored: the servers come with every request that opens the
|
|
487
|
+
// session, and their URLs can carry tokens.
|
|
488
|
+
if (mcpServers.length > 0) {
|
|
489
|
+
const entries = yield* mcpServerEntries(mcpServers, record.cwd)
|
|
490
|
+
const servers = Context.get(
|
|
491
|
+
yield* Layer.buildWithScope(McpServers.layer(entries), sessionScope),
|
|
492
|
+
McpServers.McpServers,
|
|
493
|
+
)
|
|
494
|
+
const failures = Object.entries(yield* servers.status).flatMap(
|
|
495
|
+
([name, status]) =>
|
|
496
|
+
status._tag === "Failed"
|
|
497
|
+
? [`MCP server "${name}" failed: ${status.error}`]
|
|
498
|
+
: [],
|
|
499
|
+
)
|
|
500
|
+
if (failures.length > 0) mcpFailures = failures.join("\n")
|
|
501
|
+
makeAgent = makeAgent.pipe(
|
|
502
|
+
Effect.provideService(McpServers.McpServers, servers),
|
|
503
|
+
)
|
|
504
|
+
}
|
|
505
|
+
const agent = yield* Scope.provide(makeAgent, sessionScope)
|
|
400
506
|
MutableRef.set(agent.history, record.history)
|
|
401
507
|
if (record.environment !== undefined) {
|
|
402
508
|
MutableRef.set(agent.environment, record.environment)
|
|
@@ -410,6 +516,8 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
410
516
|
running: undefined,
|
|
411
517
|
toolCalls: 0,
|
|
412
518
|
announcedSubagents: new Set(),
|
|
519
|
+
scope: sessionScope,
|
|
520
|
+
mcpFailures,
|
|
413
521
|
}
|
|
414
522
|
sessions.set(id, session)
|
|
415
523
|
return session
|
|
@@ -732,14 +840,18 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
732
840
|
agentCapabilities: {
|
|
733
841
|
loadSession: true,
|
|
734
842
|
promptCapabilities: { image: true, audio: false, embeddedContext: true },
|
|
735
|
-
mcpCapabilities: { http:
|
|
843
|
+
mcpCapabilities: { http: true, sse: false },
|
|
844
|
+
sessionCapabilities: { close: {} },
|
|
736
845
|
},
|
|
737
846
|
authMethods: [],
|
|
738
847
|
agentInfo: { name: "clanka", version: options.version },
|
|
739
848
|
})
|
|
740
849
|
|
|
741
850
|
const newSession = Effect.fnUntraced(function* (params: unknown) {
|
|
742
|
-
const { cwd, model } = yield* decodeParams(
|
|
851
|
+
const { cwd, model, mcpServers } = yield* decodeParams(
|
|
852
|
+
NewSessionParams,
|
|
853
|
+
params,
|
|
854
|
+
)
|
|
743
855
|
const modelId = model ?? options.defaultModel
|
|
744
856
|
const thoughtLevel = options.defaultThoughtLevel
|
|
745
857
|
yield* requireModel(modelId, thoughtLevel)
|
|
@@ -751,6 +863,7 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
751
863
|
thoughtLevel,
|
|
752
864
|
history: Prompt.empty,
|
|
753
865
|
}),
|
|
866
|
+
mcpServers ?? [],
|
|
754
867
|
)
|
|
755
868
|
yield* persist(session)
|
|
756
869
|
return { sessionId: session.id, ...sessionState(session) }
|
|
@@ -760,7 +873,7 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
760
873
|
params: unknown,
|
|
761
874
|
replay: boolean,
|
|
762
875
|
) {
|
|
763
|
-
const { sessionId, cwd, model } = yield* decodeParams(
|
|
876
|
+
const { sessionId, cwd, model, mcpServers } = yield* decodeParams(
|
|
764
877
|
LoadSessionParams,
|
|
765
878
|
params,
|
|
766
879
|
)
|
|
@@ -793,6 +906,7 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
793
906
|
? { environment: record.value.environment }
|
|
794
907
|
: {}),
|
|
795
908
|
}),
|
|
909
|
+
mcpServers ?? [],
|
|
796
910
|
)
|
|
797
911
|
if (replay) {
|
|
798
912
|
yield* replayHistory(session)
|
|
@@ -834,6 +948,13 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
834
948
|
// Resolve images before the turn starts, so a bad block rejects the
|
|
835
949
|
// request without a model call.
|
|
836
950
|
const input = yield* promptFromBlocks(session.cwd, prompt)
|
|
951
|
+
if (session.mcpFailures !== undefined) {
|
|
952
|
+
yield* update(sessionId, {
|
|
953
|
+
sessionUpdate: "agent_thought_chunk",
|
|
954
|
+
content: textContent(session.mcpFailures),
|
|
955
|
+
})
|
|
956
|
+
session.mcpFailures = undefined
|
|
957
|
+
}
|
|
837
958
|
session.running = yield* runTurn(session, input).pipe(
|
|
838
959
|
Effect.provide(model),
|
|
839
960
|
Effect.scoped,
|
|
@@ -847,6 +968,19 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
847
968
|
return yield* toRpcError(exit.cause)
|
|
848
969
|
})
|
|
849
970
|
|
|
971
|
+
// Ends the session in this process: its prompt, Agent and MCP servers. It
|
|
972
|
+
// can be loaded again from the store.
|
|
973
|
+
const closeSession = Effect.fnUntraced(function* (params: unknown) {
|
|
974
|
+
const { sessionId } = yield* decodeParams(SessionIdParams, params)
|
|
975
|
+
const session = yield* getSession(sessionId)
|
|
976
|
+
sessions.delete(sessionId)
|
|
977
|
+
if (session.running !== undefined) {
|
|
978
|
+
yield* Fiber.interrupt(session.running)
|
|
979
|
+
}
|
|
980
|
+
yield* Scope.close(session.scope, Exit.void)
|
|
981
|
+
return {}
|
|
982
|
+
})
|
|
983
|
+
|
|
850
984
|
const cancel = Effect.fnUntraced(function* (params: unknown) {
|
|
851
985
|
const { sessionId } = yield* decodeParams(SessionIdParams, params)
|
|
852
986
|
const running = sessions.get(sessionId)?.running
|
|
@@ -873,6 +1007,8 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
873
1007
|
return loadSession(params, true)
|
|
874
1008
|
case "session/resume":
|
|
875
1009
|
return loadSession(params, false)
|
|
1010
|
+
case "session/close":
|
|
1011
|
+
return closeSession(params)
|
|
876
1012
|
case "session/set_model":
|
|
877
1013
|
return setModel(params)
|
|
878
1014
|
case "session/prompt":
|
package/src/Agent.ts
CHANGED
|
@@ -5,6 +5,7 @@ import * as Model from "effect/ai/Model"
|
|
|
5
5
|
import type * as Response from "effect/ai/Response"
|
|
6
6
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
7
7
|
import * as AgentSkills from "./AgentSkills.ts"
|
|
8
|
+
import * as McpServers from "./McpServers.ts"
|
|
8
9
|
import { ToolkitRenderer } from "./ToolkitRenderer.ts"
|
|
9
10
|
import { stripWrappingCodeFence } from "./ScriptExtraction.ts"
|
|
10
11
|
import type * as Path from "effect/Path"
|
|
@@ -204,21 +205,25 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
204
205
|
|
|
205
206
|
const singleTool = yield* SingleTools.pipe(Effect.provide(SingleToolHandlers))
|
|
206
207
|
const capabilities = yield* executor.capabilities
|
|
208
|
+
// Shared with every sub-agent, so they use the same connections.
|
|
209
|
+
const mcpServers = Option.getOrUndefined(
|
|
210
|
+
yield* Effect.serviceOption(McpServers.McpServers),
|
|
211
|
+
)
|
|
207
212
|
const loopTools = yield* makeLoopTools({
|
|
208
213
|
loopTools: options.loopTools,
|
|
209
214
|
skills: options.skills,
|
|
210
215
|
executorTools: capabilities.tools,
|
|
211
216
|
})
|
|
212
217
|
const promptTools: PromptTools = {
|
|
213
|
-
toolsDts:
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
: `${capabilities.toolsDts}\n\n${loopTools.dts}`,
|
|
218
|
+
toolsDts: [capabilities.toolsDts, loopTools.dts, mcpServers?.dts ?? ""]
|
|
219
|
+
.filter((dts) => dts !== "")
|
|
220
|
+
.join("\n\n"),
|
|
217
221
|
offers: (name) => capabilities.tools.includes(name),
|
|
218
222
|
skillsCatalog:
|
|
219
223
|
options.skills === undefined
|
|
220
224
|
? AgentSkills.renderCatalog(capabilities.skills)
|
|
221
225
|
: AgentSkills.renderProvidedCatalog(options.skills),
|
|
226
|
+
mcp: mcpServers?.prompt ?? "",
|
|
222
227
|
}
|
|
223
228
|
|
|
224
229
|
// Steering messages for the root agent. Sub-agents get their own mailbox.
|
|
@@ -286,6 +291,15 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
286
291
|
// Images attached by `readFile` during the current script, spliced onto
|
|
287
292
|
// the Prompt as a user message once the tool result has been recorded.
|
|
288
293
|
const pendingImages: Array<ImageAttachment> = []
|
|
294
|
+
const onImage = (image: ImageAttachment) =>
|
|
295
|
+
Effect.sync(() => {
|
|
296
|
+
pendingImages.push(image)
|
|
297
|
+
})
|
|
298
|
+
// MCP results attach their images to this agent's next message.
|
|
299
|
+
const scriptLoopTools =
|
|
300
|
+
mcpServers === undefined
|
|
301
|
+
? loopTools.handlers
|
|
302
|
+
: { ...loopTools.handlers, ...mcpServers.handlers(onImage) }
|
|
289
303
|
// Whether a script ran in the current response. Their environment
|
|
290
304
|
// update is rendered once, after all of their tool results.
|
|
291
305
|
let scriptsRan = false
|
|
@@ -797,11 +811,8 @@ ${prompt}`,
|
|
|
797
811
|
}),
|
|
798
812
|
subagents,
|
|
799
813
|
onTaskComplete,
|
|
800
|
-
onImage
|
|
801
|
-
|
|
802
|
-
pendingImages.push(image)
|
|
803
|
-
}),
|
|
804
|
-
loopTools: loopTools.handlers,
|
|
814
|
+
onImage,
|
|
815
|
+
loopTools: scriptLoopTools,
|
|
805
816
|
}),
|
|
806
817
|
Stream.mkString,
|
|
807
818
|
)
|
|
@@ -1310,6 +1321,8 @@ interface PromptTools {
|
|
|
1310
1321
|
readonly toolsDts: string
|
|
1311
1322
|
readonly offers: (name: ExcludableTool) => boolean
|
|
1312
1323
|
readonly skillsCatalog: Option.Option<string>
|
|
1324
|
+
/** The MCP servers section, or `""` without servers. */
|
|
1325
|
+
readonly mcp: string
|
|
1313
1326
|
}
|
|
1314
1327
|
|
|
1315
1328
|
const when = (condition: boolean, text: string) => (condition ? text : "")
|
|
@@ -1372,7 +1385,12 @@ ${tools.toolsDts}
|
|
|
1372
1385
|
|
|
1373
1386
|
/** The global Fetch API available for making HTTP requests. */
|
|
1374
1387
|
declare const fetch: typeof globalThis.fetch
|
|
1375
|
-
\`\`\`${
|
|
1388
|
+
\`\`\`${when(
|
|
1389
|
+
tools.mcp !== "",
|
|
1390
|
+
`
|
|
1391
|
+
|
|
1392
|
+
${tools.mcp}`,
|
|
1393
|
+
)}${Option.match(tools.skillsCatalog, {
|
|
1376
1394
|
onNone: () => "",
|
|
1377
1395
|
onSome: (catalog) => `
|
|
1378
1396
|
|
|
@@ -1419,6 +1437,9 @@ const ReadSkillTools = Toolkit.make(
|
|
|
1419
1437
|
}),
|
|
1420
1438
|
)
|
|
1421
1439
|
|
|
1440
|
+
// Script names taken by MCP, whether or not servers are configured.
|
|
1441
|
+
const reservedToolNames: ReadonlySet<string> = new Set(["mcp", "searchTools"])
|
|
1442
|
+
|
|
1422
1443
|
/**
|
|
1423
1444
|
* Builds the loop-side handlers the executor puts in the sandbox, and their
|
|
1424
1445
|
* type declarations. Every call is decoded by `Toolkit.handle` before the
|
|
@@ -1432,6 +1453,13 @@ const makeLoopTools = Effect.fnUntraced(function* (options: {
|
|
|
1432
1453
|
const skills = options.skills ?? []
|
|
1433
1454
|
const callerTools = Object.keys(options.loopTools?.tools ?? {})
|
|
1434
1455
|
const names = skills.length > 0 ? [...callerTools, "readSkill"] : callerTools
|
|
1456
|
+
for (const name of [...options.executorTools, ...names]) {
|
|
1457
|
+
if (reservedToolNames.has(name)) {
|
|
1458
|
+
return yield* Effect.die(
|
|
1459
|
+
new Error(`Tool name "${name}" is reserved for MCP tools`),
|
|
1460
|
+
)
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1435
1463
|
for (const name of names) {
|
|
1436
1464
|
if (
|
|
1437
1465
|
options.executorTools.includes(name) ||
|
|
@@ -7,6 +7,7 @@ import * as Fiber from "effect/Fiber"
|
|
|
7
7
|
import * as FileSystem from "effect/FileSystem"
|
|
8
8
|
import * as Layer from "effect/Layer"
|
|
9
9
|
import * as Stream from "effect/Stream"
|
|
10
|
+
import * as TestClock from "effect/testing/TestClock"
|
|
10
11
|
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
11
12
|
import * as HttpEffect from "effect/http/HttpEffect"
|
|
12
13
|
import * as HttpServer from "effect/http/HttpServer"
|
|
@@ -414,18 +415,43 @@ try {
|
|
|
414
415
|
}).pipe(Effect.provide(executorLayer({ commandTimeout: "3 minutes" }))),
|
|
415
416
|
)
|
|
416
417
|
|
|
417
|
-
it.
|
|
418
|
+
it.effect("ends a script at the caller's script timeout", () =>
|
|
418
419
|
Effect.gen(function* () {
|
|
419
|
-
const
|
|
420
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
421
|
+
const { environment } = yield* executor.capabilities
|
|
422
|
+
const started = yield* Deferred.make<void>()
|
|
423
|
+
const script = yield* executor
|
|
424
|
+
.execute({
|
|
425
|
+
environment,
|
|
426
|
+
onEnvironment: () => Effect.void,
|
|
427
|
+
onTaskComplete: () => Effect.void,
|
|
428
|
+
onImage: () => Effect.void,
|
|
429
|
+
subagents: subagentsUnavailable,
|
|
430
|
+
script: `
|
|
420
431
|
console.log("before")
|
|
421
432
|
await sleep(10000)
|
|
422
|
-
console.log("after")
|
|
433
|
+
console.log("after")`,
|
|
434
|
+
})
|
|
435
|
+
.pipe(
|
|
436
|
+
Stream.tap((output) =>
|
|
437
|
+
output.includes("before")
|
|
438
|
+
? Deferred.succeed(started, undefined)
|
|
439
|
+
: Effect.void,
|
|
440
|
+
),
|
|
441
|
+
Stream.mkString,
|
|
442
|
+
Effect.forkScoped,
|
|
443
|
+
)
|
|
444
|
+
// Worker startup uses real time. Advance the whole-script clock only
|
|
445
|
+
// after output is observed, without changing when its deadline starts.
|
|
446
|
+
yield* Deferred.await(started)
|
|
447
|
+
yield* TestClock.adjust("300 millis")
|
|
448
|
+
const output = yield* Fiber.join(script)
|
|
423
449
|
assert.include(output, "before")
|
|
424
450
|
assert.notInclude(output, "after")
|
|
425
451
|
assert.match(output, /timed out/i)
|
|
426
452
|
}).pipe(
|
|
427
|
-
Effect.timeout("3 seconds"),
|
|
428
453
|
Effect.provide(executorLayer({ scriptTimeout: "300 millis" })),
|
|
454
|
+
Effect.scoped,
|
|
429
455
|
),
|
|
430
456
|
)
|
|
431
457
|
|
|
@@ -507,4 +533,4 @@ sleep(100).then(() => console.constructor.constructor("return process")().exit(7
|
|
|
507
533
|
),
|
|
508
534
|
)
|
|
509
535
|
})
|
|
510
|
-
}
|
|
536
|
+
}
|