clanka 0.27.0 → 0.28.1
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/dist/AgentCallerConfig.test.js +18 -0
- package/dist/AgentCallerConfig.test.js.map +1 -1
- package/dist/AgentExecutor.js +2 -0
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/McpServers.d.ts +20 -1
- package/dist/McpServers.d.ts.map +1 -1
- package/dist/McpServers.js +12 -5
- package/dist/McpServers.js.map +1 -1
- package/dist/McpServers.test.js +115 -0
- package/dist/McpServers.test.js.map +1 -1
- package/dist/bin/cli.mjs +9 -6
- package/dist/fixtures/McpFakeServer.d.ts +4 -0
- package/dist/fixtures/McpFakeServer.d.ts.map +1 -1
- package/dist/fixtures/McpFakeServer.js +13 -0
- package/dist/fixtures/McpFakeServer.js.map +1 -1
- package/package.json +1 -1
- package/src/AgentCallerConfig.test.ts +21 -0
- package/src/AgentExecutor.ts +2 -0
- package/src/McpServers.test.ts +198 -0
- package/src/McpServers.ts +44 -4
- package/src/fixtures/McpFakeServer.ts +17 -1
package/src/McpServers.test.ts
CHANGED
|
@@ -21,6 +21,7 @@ import * as Toolkit from "effect/ai/Toolkit"
|
|
|
21
21
|
import * as RpcClient from "effect/rpc/RpcClient"
|
|
22
22
|
import * as RpcSerialization from "effect/rpc/RpcSerialization"
|
|
23
23
|
import * as RpcServer from "effect/rpc/RpcServer"
|
|
24
|
+
import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
|
|
24
25
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
|
25
26
|
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"
|
|
26
27
|
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"
|
|
@@ -341,6 +342,33 @@ await taskComplete(JSON.stringify({ first, second: await attempt(mcp.remote.ping
|
|
|
341
342
|
)
|
|
342
343
|
}).pipe(Effect.scoped)
|
|
343
344
|
|
|
345
|
+
// An OAuth provider holding `token`, if any. Starting a login records the
|
|
346
|
+
// authorization URL and throws `loginError`, as a caller that cannot open a
|
|
347
|
+
// browser does.
|
|
348
|
+
const oauth = (options: {
|
|
349
|
+
readonly token?: string
|
|
350
|
+
readonly loginError: string
|
|
351
|
+
}) => {
|
|
352
|
+
const logins: Array<URL> = []
|
|
353
|
+
const provider: OAuthClientProvider = {
|
|
354
|
+
redirectUrl: "http://127.0.0.1/callback",
|
|
355
|
+
clientMetadata: { redirect_uris: ["http://127.0.0.1/callback"] },
|
|
356
|
+
clientInformation: () => ({ client_id: "clanka-test" }),
|
|
357
|
+
tokens: () =>
|
|
358
|
+
options.token === undefined
|
|
359
|
+
? undefined
|
|
360
|
+
: { access_token: options.token, token_type: "Bearer" },
|
|
361
|
+
saveTokens: () => {},
|
|
362
|
+
saveCodeVerifier: () => {},
|
|
363
|
+
codeVerifier: () => "verifier",
|
|
364
|
+
redirectToAuthorization: (url) => {
|
|
365
|
+
logins.push(url)
|
|
366
|
+
throw new Error(options.loginError)
|
|
367
|
+
},
|
|
368
|
+
}
|
|
369
|
+
return { provider, logins }
|
|
370
|
+
}
|
|
371
|
+
|
|
344
372
|
// --- lifecycle and prompt ----------------------------------------------------
|
|
345
373
|
|
|
346
374
|
describe("McpServers", () => {
|
|
@@ -1047,4 +1075,174 @@ console.log(await mcp.fake.screenshot({}))`
|
|
|
1047
1075
|
}
|
|
1048
1076
|
}).pipe(Effect.scoped),
|
|
1049
1077
|
)
|
|
1078
|
+
|
|
1079
|
+
// --- OAuth -------------------------------------------------------------------
|
|
1080
|
+
|
|
1081
|
+
it.live("http servers are sent the OAuth provider's token", () =>
|
|
1082
|
+
Effect.gen(function* () {
|
|
1083
|
+
const remote = yield* Effect.acquireRelease(
|
|
1084
|
+
Effect.promise(() =>
|
|
1085
|
+
listenHttp(makeFakeServer({ tools: named("ping") }), "/mcp", {
|
|
1086
|
+
authorize: (header) => header === "Bearer good",
|
|
1087
|
+
}),
|
|
1088
|
+
),
|
|
1089
|
+
(remote) => Effect.promise(remote.close),
|
|
1090
|
+
)
|
|
1091
|
+
const { provider, logins } = oauth({
|
|
1092
|
+
token: "good",
|
|
1093
|
+
loginError: "login required",
|
|
1094
|
+
})
|
|
1095
|
+
const asked: Array<string> = []
|
|
1096
|
+
const { summary, status } = yield* Effect.gen(function* () {
|
|
1097
|
+
const { summary } = yield* runAgent({
|
|
1098
|
+
script: "await taskComplete(await mcp.remote.ping({}))",
|
|
1099
|
+
})
|
|
1100
|
+
return { summary, status: yield* currentStatus }
|
|
1101
|
+
}).pipe(
|
|
1102
|
+
Effect.provide(
|
|
1103
|
+
McpServers.layer(
|
|
1104
|
+
{ remote: { type: "http", url: remote.url } },
|
|
1105
|
+
(name, config) => {
|
|
1106
|
+
asked.push(`${name} ${config.url}`)
|
|
1107
|
+
return provider
|
|
1108
|
+
},
|
|
1109
|
+
),
|
|
1110
|
+
),
|
|
1111
|
+
)
|
|
1112
|
+
assert.strictEqual(summary, "ping")
|
|
1113
|
+
assert.strictEqual(status["remote"]?._tag, "Connected")
|
|
1114
|
+
assert.deepStrictEqual(asked, [`remote ${remote.url}`])
|
|
1115
|
+
assert.isEmpty(logins)
|
|
1116
|
+
assert.isNotEmpty(remote.authorization)
|
|
1117
|
+
for (const header of remote.authorization) {
|
|
1118
|
+
assert.strictEqual(header, "Bearer good")
|
|
1119
|
+
}
|
|
1120
|
+
}).pipe(Effect.scoped),
|
|
1121
|
+
)
|
|
1122
|
+
|
|
1123
|
+
it.live(
|
|
1124
|
+
"a login the provider cannot start fails the server with its error",
|
|
1125
|
+
() =>
|
|
1126
|
+
Effect.gen(function* () {
|
|
1127
|
+
const remote = yield* Effect.acquireRelease(
|
|
1128
|
+
Effect.promise(() =>
|
|
1129
|
+
listenHttp(makeFakeServer({ tools: named("ping") }), "/mcp", {
|
|
1130
|
+
authorize: () => false,
|
|
1131
|
+
}),
|
|
1132
|
+
),
|
|
1133
|
+
(remote) => Effect.promise(remote.close),
|
|
1134
|
+
)
|
|
1135
|
+
const { provider, logins } = oauth({
|
|
1136
|
+
loginError: "remote needs login\nopen the browser to continue",
|
|
1137
|
+
})
|
|
1138
|
+
const { system, status } = yield* Effect.gen(function* () {
|
|
1139
|
+
const { system } = yield* runAgent({
|
|
1140
|
+
script: "await taskComplete(typeof mcp)",
|
|
1141
|
+
})
|
|
1142
|
+
return { system, status: yield* currentStatus }
|
|
1143
|
+
}).pipe(
|
|
1144
|
+
Effect.provide(
|
|
1145
|
+
McpServers.layer(
|
|
1146
|
+
{ remote: { type: "http", url: remote.url } },
|
|
1147
|
+
() => provider,
|
|
1148
|
+
),
|
|
1149
|
+
),
|
|
1150
|
+
)
|
|
1151
|
+
assert.strictEqual(
|
|
1152
|
+
failure(status, "remote"),
|
|
1153
|
+
"remote needs login\nopen the browser to continue",
|
|
1154
|
+
)
|
|
1155
|
+
// The prompt shows the first line.
|
|
1156
|
+
assert.include(
|
|
1157
|
+
system,
|
|
1158
|
+
"- remote: failed to connect: remote needs login",
|
|
1159
|
+
)
|
|
1160
|
+
assert.notInclude(system, "open the browser")
|
|
1161
|
+
assert.strictEqual(logins.length, 1)
|
|
1162
|
+
assert.strictEqual(logins[0]!.pathname, "/authorize")
|
|
1163
|
+
}).pipe(Effect.scoped),
|
|
1164
|
+
)
|
|
1165
|
+
|
|
1166
|
+
it.live(
|
|
1167
|
+
"a token rejected during a call fails the call with the provider's error",
|
|
1168
|
+
() =>
|
|
1169
|
+
Effect.gen(function* () {
|
|
1170
|
+
let revoked = false
|
|
1171
|
+
const remote = yield* Effect.acquireRelease(
|
|
1172
|
+
Effect.promise(() =>
|
|
1173
|
+
listenHttp(
|
|
1174
|
+
makeFakeServer({
|
|
1175
|
+
tools: [
|
|
1176
|
+
{ name: "ping", call: () => text("pong") },
|
|
1177
|
+
{
|
|
1178
|
+
name: "revoke",
|
|
1179
|
+
call: () => {
|
|
1180
|
+
revoked = true
|
|
1181
|
+
return text("revoked")
|
|
1182
|
+
},
|
|
1183
|
+
},
|
|
1184
|
+
],
|
|
1185
|
+
}),
|
|
1186
|
+
"/mcp",
|
|
1187
|
+
{ authorize: (header) => header === "Bearer good" && !revoked },
|
|
1188
|
+
),
|
|
1189
|
+
),
|
|
1190
|
+
(remote) => Effect.promise(remote.close),
|
|
1191
|
+
)
|
|
1192
|
+
const other = yield* Effect.acquireRelease(
|
|
1193
|
+
Effect.promise(() =>
|
|
1194
|
+
listenHttp(makeFakeServer({ tools: named("ping") })),
|
|
1195
|
+
),
|
|
1196
|
+
(other) => Effect.promise(other.close),
|
|
1197
|
+
)
|
|
1198
|
+
const { provider, logins } = oauth({
|
|
1199
|
+
token: "good",
|
|
1200
|
+
loginError: "remote needs login",
|
|
1201
|
+
})
|
|
1202
|
+
const { summary, status } = yield* Effect.gen(function* () {
|
|
1203
|
+
const { summary } = yield* runAgent({
|
|
1204
|
+
script: `${attempt}
|
|
1205
|
+
await mcp.remote.revoke({})
|
|
1206
|
+
const remote = await attempt(mcp.remote.ping({}))
|
|
1207
|
+
await taskComplete(JSON.stringify({ remote, other: await attempt(mcp.other.ping({})) }))`,
|
|
1208
|
+
})
|
|
1209
|
+
return { summary, status: yield* currentStatus }
|
|
1210
|
+
}).pipe(
|
|
1211
|
+
Effect.provide(
|
|
1212
|
+
McpServers.layer(
|
|
1213
|
+
{
|
|
1214
|
+
remote: { type: "http", url: remote.url },
|
|
1215
|
+
other: { type: "http", url: other.url },
|
|
1216
|
+
},
|
|
1217
|
+
(name) => (name === "remote" ? provider : undefined),
|
|
1218
|
+
),
|
|
1219
|
+
),
|
|
1220
|
+
)
|
|
1221
|
+
assert.deepStrictEqual(JSON.parse(summary), {
|
|
1222
|
+
remote: "ERR mcp.remote.ping: remote needs login",
|
|
1223
|
+
other: "ping",
|
|
1224
|
+
})
|
|
1225
|
+
assert.strictEqual(logins.length, 1)
|
|
1226
|
+
assert.strictEqual(status["other"]?._tag, "Connected")
|
|
1227
|
+
}).pipe(Effect.scoped),
|
|
1228
|
+
)
|
|
1229
|
+
|
|
1230
|
+
it.live("without an OAuth provider a 401 fails the server", () =>
|
|
1231
|
+
Effect.gen(function* () {
|
|
1232
|
+
const remote = yield* Effect.acquireRelease(
|
|
1233
|
+
Effect.promise(() =>
|
|
1234
|
+
listenHttp(makeFakeServer({ tools: named("ping") }), "/mcp", {
|
|
1235
|
+
authorize: () => false,
|
|
1236
|
+
}),
|
|
1237
|
+
),
|
|
1238
|
+
(remote) => Effect.promise(remote.close),
|
|
1239
|
+
)
|
|
1240
|
+
const status = yield* currentStatus.pipe(
|
|
1241
|
+
Effect.provide(
|
|
1242
|
+
McpServers.layer({ remote: { type: "http", url: remote.url } }),
|
|
1243
|
+
),
|
|
1244
|
+
)
|
|
1245
|
+
assert.include(failure(status, "remote"), "Streamable HTTP error")
|
|
1246
|
+
}).pipe(Effect.scoped),
|
|
1247
|
+
)
|
|
1050
1248
|
})
|
package/src/McpServers.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import * as NodePath from "node:path"
|
|
11
11
|
import type { Readable } from "node:stream"
|
|
12
|
+
import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
|
|
12
13
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
13
14
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
|
|
14
15
|
import {
|
|
@@ -86,6 +87,26 @@ export const HttpServerConfig = Schema.Struct({
|
|
|
86
87
|
callTimeout: Schema.optionalKey(Timeout),
|
|
87
88
|
})
|
|
88
89
|
|
|
90
|
+
/**
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
* @category Config
|
|
93
|
+
*/
|
|
94
|
+
export type HttpServerConfig = typeof HttpServerConfig.Type
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Chooses the OAuth provider for an HTTP server, or none. The provider is
|
|
98
|
+
* passed to the SDK transport, which sends its token and calls it when the
|
|
99
|
+
* server answers 401. An error the provider throws fails the connection or
|
|
100
|
+
* the tool call with its message.
|
|
101
|
+
*
|
|
102
|
+
* @since 1.0.0
|
|
103
|
+
* @category Config
|
|
104
|
+
*/
|
|
105
|
+
export type AuthProviderFor = (
|
|
106
|
+
name: string,
|
|
107
|
+
config: HttpServerConfig,
|
|
108
|
+
) => OAuthClientProvider | undefined
|
|
109
|
+
|
|
89
110
|
/**
|
|
90
111
|
* @since 1.0.0
|
|
91
112
|
* @category Config
|
|
@@ -202,14 +223,20 @@ export class McpServers extends Context.Service<
|
|
|
202
223
|
* Connects every server when built and closes them with its scope. A server
|
|
203
224
|
* that cannot connect is `Failed`; building the layer never fails.
|
|
204
225
|
*
|
|
226
|
+
* `authProvider` is called once for each HTTP server, when the layer is
|
|
227
|
+
* built.
|
|
228
|
+
*
|
|
205
229
|
* @since 1.0.0
|
|
206
230
|
* @category Layers
|
|
207
231
|
*/
|
|
208
232
|
export const layer = (
|
|
209
233
|
config: Readonly<Record<string, ServerEntry>>,
|
|
234
|
+
authProvider?: AuthProviderFor | undefined,
|
|
210
235
|
): Layer.Layer<McpServers> =>
|
|
211
236
|
fromEntries(
|
|
212
|
-
Object.entries(config).map(([name, entry]) =>
|
|
237
|
+
Object.entries(config).map(([name, entry]) =>
|
|
238
|
+
toEntry(name, entry, authProvider),
|
|
239
|
+
),
|
|
213
240
|
)
|
|
214
241
|
|
|
215
242
|
/**
|
|
@@ -318,7 +345,11 @@ type Entry =
|
|
|
318
345
|
}
|
|
319
346
|
| { readonly name: string; readonly error: string }
|
|
320
347
|
|
|
321
|
-
const toEntry = (
|
|
348
|
+
const toEntry = (
|
|
349
|
+
name: string,
|
|
350
|
+
entry: ServerEntry,
|
|
351
|
+
authProvider: AuthProviderFor | undefined,
|
|
352
|
+
): Entry => {
|
|
322
353
|
switch (entry.type) {
|
|
323
354
|
case "unavailable":
|
|
324
355
|
return { name, error: entry.error }
|
|
@@ -348,6 +379,7 @@ const toEntry = (name: string, entry: ServerEntry): Entry => {
|
|
|
348
379
|
transport: new HttpTransport(
|
|
349
380
|
new URL(entry.url),
|
|
350
381
|
unwrapRecord(entry.headers),
|
|
382
|
+
authProvider?.(name, entry),
|
|
351
383
|
) as Transport,
|
|
352
384
|
stderr: undefined,
|
|
353
385
|
}),
|
|
@@ -365,7 +397,11 @@ const toEntry = (name: string, entry: ServerEntry): Entry => {
|
|
|
365
397
|
class HttpTransport extends StreamableHTTPClientTransport {
|
|
366
398
|
readonly reconnection: { maxRetries: number }
|
|
367
399
|
|
|
368
|
-
constructor(
|
|
400
|
+
constructor(
|
|
401
|
+
url: URL,
|
|
402
|
+
headers: Record<string, string>,
|
|
403
|
+
authProvider: OAuthClientProvider | undefined,
|
|
404
|
+
) {
|
|
369
405
|
// The SDK defaults.
|
|
370
406
|
const reconnection = {
|
|
371
407
|
initialReconnectionDelay: 1000,
|
|
@@ -373,7 +409,11 @@ class HttpTransport extends StreamableHTTPClientTransport {
|
|
|
373
409
|
reconnectionDelayGrowFactor: 1.5,
|
|
374
410
|
maxRetries: 2,
|
|
375
411
|
}
|
|
376
|
-
super(url, {
|
|
412
|
+
super(url, {
|
|
413
|
+
requestInit: { headers },
|
|
414
|
+
reconnectionOptions: reconnection,
|
|
415
|
+
...(authProvider === undefined ? {} : { authProvider }),
|
|
416
|
+
})
|
|
377
417
|
this.reconnection = reconnection
|
|
378
418
|
}
|
|
379
419
|
|
|
@@ -98,11 +98,17 @@ export const makeFakeServer = (options: {
|
|
|
98
98
|
* - `endSession` answers every later request with 404, as a server does for
|
|
99
99
|
* a session it has ended. `afterEnd` lists those requests as
|
|
100
100
|
* `"<HTTP method> <JSON-RPC method>"`.
|
|
101
|
+
* - `authorize`, when given, answers 401 to a request whose `Authorization`
|
|
102
|
+
* header it rejects, and 404 to any other path, so OAuth discovery finds no
|
|
103
|
+
* metadata.
|
|
101
104
|
*/
|
|
102
105
|
export const listenHttp = async (
|
|
103
106
|
server: Server,
|
|
104
107
|
path = "/mcp",
|
|
105
|
-
options: {
|
|
108
|
+
options: {
|
|
109
|
+
readonly resumable?: boolean
|
|
110
|
+
readonly authorize?: (authorization: string | undefined) => boolean
|
|
111
|
+
} = {},
|
|
106
112
|
): Promise<{
|
|
107
113
|
readonly url: string
|
|
108
114
|
readonly authorization: ReadonlyArray<string | undefined>
|
|
@@ -123,6 +129,16 @@ export const listenHttp = async (
|
|
|
123
129
|
let ended = false
|
|
124
130
|
const http = Http.createServer((request, response) => {
|
|
125
131
|
authorization.push(request.headers.authorization)
|
|
132
|
+
if (options.authorize !== undefined) {
|
|
133
|
+
if (new URL(request.url ?? "/", "http://localhost").pathname !== path) {
|
|
134
|
+
response.writeHead(404).end()
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
if (!options.authorize(request.headers.authorization)) {
|
|
138
|
+
response.writeHead(401, { "www-authenticate": "Bearer" }).end()
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
}
|
|
126
142
|
if (!ended) {
|
|
127
143
|
void transport.handleRequest(request, response)
|
|
128
144
|
return
|