@effected/mcp 0.11.0 → 0.13.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 +5 -562
- package/bin/effected-mcp.js +12 -0
- package/index.d.ts +11 -929
- package/index.js +2 -9
- package/main.d.ts +27 -0
- package/main.js +37 -0
- package/package.json +15 -21
- package/server.js +39 -0
- package/tools/identity.js +28 -0
- package/tsdoc-metadata.json +1 -1
- package/version.js +11 -0
- package/McpGuard.js +0 -81
- package/McpHarness.js +0 -338
- package/McpProbe.js +0 -94
- package/McpProcess.js +0 -196
- package/McpStdio.js +0 -159
- package/McpTestFailure.js +0 -49
- package/McpToolAudit.js +0 -109
- package/McpToolkit.js +0 -246
- package/StdioCapture.js +0 -146
- package/ToolFailure.js +0 -75
- package/ToolInputSchema.js +0 -180
- package/ToolOutcome.js +0 -119
- package/ToolOutputSchema.js +0 -65
- package/ToolRefusal.js +0 -57
- package/ToolReplay.js +0 -122
- package/guard.d.ts +0 -189
- package/guard.js +0 -3
- package/internal/LaunchFailed.js +0 -34
- package/internal/StdinFrames.js +0 -201
- package/internal/guardLaunch.js +0 -13
- package/internal/wire.js +0 -65
- package/node.d.ts +0 -126
- package/node.js +0 -3
- package/testing.d.ts +0 -490
- package/testing.js +0 -7
package/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ToolFailure } from "./ToolFailure.js";
|
|
3
|
-
import { ToolInputSchema } from "./ToolInputSchema.js";
|
|
4
|
-
import { McpToolkit } from "./McpToolkit.js";
|
|
5
|
-
import { ToolOutputSchema } from "./ToolOutputSchema.js";
|
|
6
|
-
import { ToolOutcome } from "./ToolOutcome.js";
|
|
7
|
-
import { ToolRefusal } from "./ToolRefusal.js";
|
|
8
|
-
import { ToolReplay } from "./ToolReplay.js";
|
|
1
|
+
import { MCP_VERSION } from "./version.js";
|
|
9
2
|
|
|
10
|
-
export {
|
|
3
|
+
export { MCP_VERSION };
|
package/main.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Distribution } from "@effected/kit";
|
|
2
|
+
/**
|
|
3
|
+
* Options a carrier's bin shim passes to `main`. `distribution` names
|
|
4
|
+
* the package the `effected-mcp` bin was installed through; omitted for a
|
|
5
|
+
* direct install of `@effected/mcp`.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export interface MainOptions {
|
|
10
|
+
readonly distribution?: Distribution;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Run the `effected-mcp` server over stdio. Owns the process.
|
|
14
|
+
*
|
|
15
|
+
* `McpGuard.run` installs the crash guards before `load` evaluates the server
|
|
16
|
+
* graph, so a throw during module evaluation is still reported on stderr; this
|
|
17
|
+
* module therefore carries no static runtime import (`Distribution` is
|
|
18
|
+
* type-only). Policy is `exitBeforeConnect` for both events: the one tool is
|
|
19
|
+
* read-only and stateless, so a stray error while serving has no state to
|
|
20
|
+
* corrupt and the guard logs and keeps serving, while the same error before
|
|
21
|
+
* serving means a broken boot and exits `1`. Nothing here writes to stdout;
|
|
22
|
+
* it is the JSON-RPC wire.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare const main: (options?: MainOptions) => Promise<void>;
|
|
27
|
+
//# sourceMappingURL=main.d.ts.map
|
package/main.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { McpGuard } from "@effected/mcp-kit/guard";
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
/**
|
|
5
|
+
* Run the `effected-mcp` server over stdio. Owns the process.
|
|
6
|
+
*
|
|
7
|
+
* `McpGuard.run` installs the crash guards before `load` evaluates the server
|
|
8
|
+
* graph, so a throw during module evaluation is still reported on stderr; this
|
|
9
|
+
* module therefore carries no static runtime import (`Distribution` is
|
|
10
|
+
* type-only). Policy is `exitBeforeConnect` for both events: the one tool is
|
|
11
|
+
* read-only and stateless, so a stray error while serving has no state to
|
|
12
|
+
* corrupt and the guard logs and keeps serving, while the same error before
|
|
13
|
+
* serving means a broken boot and exits `1`. Nothing here writes to stdout;
|
|
14
|
+
* it is the JSON-RPC wire.
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
const main = (options = {}) => McpGuard.run({
|
|
19
|
+
label: "effected-mcp",
|
|
20
|
+
host: process,
|
|
21
|
+
policy: {
|
|
22
|
+
onUncaught: "exitBeforeConnect",
|
|
23
|
+
onRejection: "exitBeforeConnect"
|
|
24
|
+
},
|
|
25
|
+
load: async () => {
|
|
26
|
+
const NodeRuntime = await import("@effect/platform-node/NodeRuntime");
|
|
27
|
+
const NodeStdio = await import("@effect/platform-node/NodeStdio");
|
|
28
|
+
const { Layer } = await import("effect");
|
|
29
|
+
const { ServerLayer } = await import("./server.js");
|
|
30
|
+
return {
|
|
31
|
+
layer: ServerLayer(options).pipe(Layer.provide(NodeStdio.layer)),
|
|
32
|
+
runMain: NodeRuntime.runMain
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export { main };
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effected/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "The
|
|
5
|
+
"description": "The effected-mcp server: the tool's MCP tools on the kit's MCP boundary",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"effect",
|
|
8
8
|
"mcp",
|
|
9
9
|
"model-context-protocol",
|
|
10
|
-
"stdio",
|
|
11
|
-
"json-rpc",
|
|
12
10
|
"effected"
|
|
13
11
|
],
|
|
14
12
|
"homepage": "https://github.com/spencerbeggs/effected/tree/main/packages/mcp#readme",
|
|
@@ -34,26 +32,22 @@
|
|
|
34
32
|
"import": "./index.js",
|
|
35
33
|
"default": "./index.js"
|
|
36
34
|
},
|
|
37
|
-
"./
|
|
38
|
-
"types": "./
|
|
39
|
-
"import": "./
|
|
40
|
-
"default": "./
|
|
41
|
-
},
|
|
42
|
-
"./node": {
|
|
43
|
-
"types": "./node.d.ts",
|
|
44
|
-
"import": "./node.js",
|
|
45
|
-
"default": "./node.js"
|
|
46
|
-
},
|
|
47
|
-
"./testing": {
|
|
48
|
-
"types": "./testing.d.ts",
|
|
49
|
-
"import": "./testing.js",
|
|
50
|
-
"default": "./testing.js"
|
|
35
|
+
"./main": {
|
|
36
|
+
"types": "./main.d.ts",
|
|
37
|
+
"import": "./main.js",
|
|
38
|
+
"default": "./main.js"
|
|
51
39
|
},
|
|
52
40
|
"./package.json": "./package.json"
|
|
53
41
|
},
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
|
|
42
|
+
"bin": {
|
|
43
|
+
"effected-mcp": "bin/effected-mcp.js"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@effect/platform-node": "~4.1.2",
|
|
47
|
+
"@effected/engine": "^0.12.0",
|
|
48
|
+
"@effected/kit": "^0.12.0",
|
|
49
|
+
"@effected/mcp-kit": "^0.13.0",
|
|
50
|
+
"effect": "~4.1.2"
|
|
57
51
|
},
|
|
58
52
|
"engines": {
|
|
59
53
|
"node": ">=24.11.0"
|
package/server.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { handleIdentity, identity } from "./tools/identity.js";
|
|
2
|
+
import { MCP_VERSION } from "./version.js";
|
|
3
|
+
import { McpStdio, McpToolkit } from "@effected/mcp-kit";
|
|
4
|
+
import { Layer } from "effect";
|
|
5
|
+
import { McpProtocol, Toolkit } from "effect/ai";
|
|
6
|
+
|
|
7
|
+
// src/server.ts
|
|
8
|
+
/**
|
|
9
|
+
* Agent-facing orientation, surfaced as `instructions` in both the
|
|
10
|
+
* `initialize` and `server/discover` results.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
const SERVER_INSTRUCTIONS = "effected-mcp serves the effected tool. Call identity to learn the tool name, this server's version, the distribution it was launched through and the resolved effect version.";
|
|
15
|
+
/**
|
|
16
|
+
* The whole server as one layer: the `identity` toolkit over the kit's
|
|
17
|
+
* `McpStdio.layer`. The adapters are listed newest first, as the kit's
|
|
18
|
+
* default lists them; a client that opens with `initialize` needs the
|
|
19
|
+
* stateful ones.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
const ServerLayer = (options = {}) => {
|
|
24
|
+
const EffectedToolkit = Toolkit.make(identity);
|
|
25
|
+
const handlers = EffectedToolkit.toLayer({ identity: () => handleIdentity(options.distribution) });
|
|
26
|
+
return McpStdio.layer({
|
|
27
|
+
name: "effected",
|
|
28
|
+
version: MCP_VERSION,
|
|
29
|
+
instructions: SERVER_INSTRUCTIONS,
|
|
30
|
+
protocols: [
|
|
31
|
+
McpProtocol.v2026_07_28,
|
|
32
|
+
McpProtocol.v2025_11_25,
|
|
33
|
+
McpProtocol.v2025_06_18
|
|
34
|
+
],
|
|
35
|
+
registrations: McpToolkit.layer(EffectedToolkit).pipe(Layer.provide(handlers))
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { SERVER_INSTRUCTIONS, ServerLayer };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { MCP_VERSION } from "../version.js";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Tool } from "effect/ai";
|
|
4
|
+
import { Identity, IdentityReport } from "@effected/engine";
|
|
5
|
+
|
|
6
|
+
// src/tools/identity.ts
|
|
7
|
+
/**
|
|
8
|
+
* The `identity` tool: what the `effected` tool reports about itself, the
|
|
9
|
+
* same report the `effected` command prints.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
const identity = Tool.make("identity", {
|
|
14
|
+
description: "Reports the effected tool's identity: the tool name, this server's version, the carrier distribution it was launched through (null for a direct install), and the resolved effect version.",
|
|
15
|
+
parameters: Tool.EmptyParams,
|
|
16
|
+
success: IdentityReport
|
|
17
|
+
}).annotate(Tool.Title, "Effected identity").annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true).annotate(Tool.OpenWorld, false);
|
|
18
|
+
/**
|
|
19
|
+
* The `identity` handler, closed over the carrier distribution.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
const handleIdentity = (distribution) => Effect.suspend(() => Identity.report({
|
|
24
|
+
version: MCP_VERSION,
|
|
25
|
+
distribution: distribution ?? null
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
export { handleIdentity, identity };
|
package/tsdoc-metadata.json
CHANGED
package/version.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/version.ts
|
|
2
|
+
/**
|
|
3
|
+
* The `effected-mcp` server's version. The bundler replaces the exact expression
|
|
4
|
+
* `process.env.__PACKAGE_VERSION__` with the package version at build time, so
|
|
5
|
+
* it must stay spelled this way.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
const MCP_VERSION = "0.13.0";
|
|
10
|
+
|
|
11
|
+
export { MCP_VERSION };
|
package/McpGuard.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { ProcessGuard } from "@effected/engine/guard";
|
|
2
|
-
|
|
3
|
-
// src/McpGuard.ts
|
|
4
|
-
/**
|
|
5
|
-
* Crash guards for an MCP server process, installed before the server's
|
|
6
|
-
* module graph is loaded. Imported from `@effected/mcp/guard`.
|
|
7
|
-
*
|
|
8
|
-
* @remarks
|
|
9
|
-
* {@link McpGuard.run} is `ProcessGuard.run` from `@effected/engine/guard`
|
|
10
|
-
* with an MCP launch in its `load`: it registers `uncaughtException` and
|
|
11
|
-
* `unhandledRejection` listeners on `host`, then awaits `load()`, then
|
|
12
|
-
* launches the server with `McpStdio.launch` and `McpStdio.teardown` under
|
|
13
|
-
* the `runMain` it returned. A server on another transport (an LSP over
|
|
14
|
-
* `vscode-languageserver`, say) uses `ProcessGuard.run` directly and calls
|
|
15
|
-
* its `markConnected` itself. This entrypoint statically imports only
|
|
16
|
-
* `@effected/engine/guard`, which imports nothing: it loads `effect` and the
|
|
17
|
-
* stdio wiring only after the guards are listening, so a throw while any of
|
|
18
|
-
* it evaluates is still reported on stderr.
|
|
19
|
-
*
|
|
20
|
-
* - "Connected" means the whole server layer has built, so stdin is being
|
|
21
|
-
* read. That is before any client sends `initialize`.
|
|
22
|
-
* - `injectCrash` drives either half of the policy from a test, before
|
|
23
|
-
* `load()` or once connected; unset, it does nothing.
|
|
24
|
-
* - A `load()` that rejects is reported as `startup failed` and exits 1
|
|
25
|
-
* whatever the policy. Left to a log-only rejection listener, it would
|
|
26
|
-
* let the event loop drain and exit 0 with no server.
|
|
27
|
-
* - Installing an `unhandledRejection` listener switches off Node's default
|
|
28
|
-
* of throwing on one, so `"log"` really does keep the process running.
|
|
29
|
-
* - An exit from a guard skips Effect's finalizers and the teardown.
|
|
30
|
-
* - Keeping on serving after a crash works because the platform `runMain`
|
|
31
|
-
* holds the process open while the server fiber lives.
|
|
32
|
-
* - A layer that fails to build is still reported by `McpStdio.launch`.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* import { McpGuard } from "@effected/mcp/guard";
|
|
37
|
-
*
|
|
38
|
-
* await McpGuard.run({
|
|
39
|
-
* label: "my-server",
|
|
40
|
-
* host: process,
|
|
41
|
-
* policy: { onUncaught: "exitBeforeConnect", onRejection: "log" },
|
|
42
|
-
* load: async () => {
|
|
43
|
-
* const { NodeRuntime } = await import("@effect/platform-node");
|
|
44
|
-
* const { Main } = await import("./server.js");
|
|
45
|
-
* return { layer: Main, runMain: NodeRuntime.runMain };
|
|
46
|
-
* },
|
|
47
|
-
* });
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @public
|
|
51
|
-
*/
|
|
52
|
-
var McpGuard = class {
|
|
53
|
-
constructor() {}
|
|
54
|
-
/**
|
|
55
|
-
* Parse a test-only crash-injection setting into {@link McpGuardRunOptions.injectCrash}: `<at>:<kind>`, where `at`
|
|
56
|
-
* is `"load"` or `"connected"` and `kind` is `"uncaughtException"` or `"unhandledRejection"`. Anything else, or no
|
|
57
|
-
* value, is `undefined`. The same grammar as `ProcessGuard.parseInjectCrash` from `@effected/engine/guard`, which
|
|
58
|
-
* this is, so an MCP launcher need not import the engine guard for it.
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* McpGuard.parseInjectCrash("load:uncaughtException"); // => { at: "load", kind: "uncaughtException" }
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
static parseInjectCrash = ProcessGuard.parseInjectCrash;
|
|
66
|
-
/** Install the guards, load the server, and launch it. Resolves once the server is launched. */
|
|
67
|
-
static run = (options) => ProcessGuard.run({
|
|
68
|
-
label: options.label,
|
|
69
|
-
host: options.host,
|
|
70
|
-
policy: options.policy,
|
|
71
|
-
injectCrash: options.injectCrash,
|
|
72
|
-
load: async (guard) => {
|
|
73
|
-
const server = await options.load();
|
|
74
|
-
if (server.format !== void 0) guard.useFormat(server.format);
|
|
75
|
-
const { launchGuarded } = await import("./internal/guardLaunch.js");
|
|
76
|
-
launchGuarded(server, guard.markConnected);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export { McpGuard };
|
package/McpHarness.js
DELETED
|
@@ -1,338 +0,0 @@
|
|
|
1
|
-
import { ToolFailure } from "./ToolFailure.js";
|
|
2
|
-
import { DEFAULT_CLIENT_INFO, frame, initializeParams, isJsonRpcMessage, isResponse, isStateless, parseFrame, requestKey } from "./internal/wire.js";
|
|
3
|
-
import { McpTestFailure } from "./McpTestFailure.js";
|
|
4
|
-
import { Console, Deferred, Effect, Exit, Fiber, Layer, Queue, Sink, Stdio, Stream } from "effect";
|
|
5
|
-
import { McpProtocol } from "effect/ai";
|
|
6
|
-
import { ErrorText } from "@effected/engine";
|
|
7
|
-
|
|
8
|
-
// src/McpHarness.ts
|
|
9
|
-
/** The generic tag `Object.prototype.toString` answers with: a value with no string form of its own. */
|
|
10
|
-
const GENERIC_STRING = /^\[object [^\]]*\]$/;
|
|
11
|
-
/**
|
|
12
|
-
* One captured console argument as text, as close to what the console would show as a runtime-free rendering gets,
|
|
13
|
-
* and never throwing.
|
|
14
|
-
*
|
|
15
|
-
* @remarks
|
|
16
|
-
* A harness records what the server would have printed, so the record must not lose what the console shows. Hence
|
|
17
|
-
* the order: a value's own string form first (`String(error)` is `Error: boom`, where `ErrorText.describe` would keep
|
|
18
|
-
* only `boom`, and it is the same for any object carrying a `message`), then JSON for an array or a plain object,
|
|
19
|
-
* whose string form is `1,2` or `[object Object]`. `ErrorText.describe` is the last resort for a value whose string
|
|
20
|
-
* form throws and which has no JSON: an object with no prototype has no `toString`, and `String` throws on it inside
|
|
21
|
-
* the server's own console call.
|
|
22
|
-
*/
|
|
23
|
-
const showArg = (arg) => {
|
|
24
|
-
if (typeof arg === "string") return arg;
|
|
25
|
-
if (typeof arg !== "object" || arg === null) return ErrorText.describe(arg);
|
|
26
|
-
try {
|
|
27
|
-
if (!Array.isArray(arg)) {
|
|
28
|
-
const text = String(arg);
|
|
29
|
-
if (!GENERIC_STRING.test(text)) return text;
|
|
30
|
-
}
|
|
31
|
-
} catch {}
|
|
32
|
-
try {
|
|
33
|
-
const json = JSON.stringify(arg);
|
|
34
|
-
if (json !== void 0) return json;
|
|
35
|
-
} catch {}
|
|
36
|
-
return ErrorText.describe(arg);
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* An in-process MCP client for a real server layer, over queue-backed stdio.
|
|
40
|
-
*
|
|
41
|
-
* @remarks
|
|
42
|
-
* Builds your server with a queue-backed `Stdio.layerTest` that satisfies the
|
|
43
|
-
* server's `Stdio` requirement, so tests see the exact served schemas and wire
|
|
44
|
-
* results a real client would, with no child and no sockets. Pass the server
|
|
45
|
-
* WITHOUT a `Stdio` of its own: a `Stdio` the server provides internally wins,
|
|
46
|
-
* and talks to the real terminal. A bundled platform layer such as
|
|
47
|
-
* `NodeServices.layer` (`@effect/platform-node`) brings its own `Stdio` along
|
|
48
|
-
* with `FileSystem`, `Path`, `ChildProcessSpawner`, `Crypto` and `Terminal` —
|
|
49
|
-
* providing it to the server loses `Stdio` the same way, and every wait
|
|
50
|
-
* hangs to timeout with no error naming why. Compose the individual layers
|
|
51
|
-
* the server needs instead and leave `Stdio` out; this harness supplies it.
|
|
52
|
-
*
|
|
53
|
-
* The server is built with a fresh layer memo map, never the ambient one, so
|
|
54
|
-
* a harness made under another stdio server's `Effect.provide` still serves
|
|
55
|
-
* its own. Never pass a server layer that provides a layer the test also
|
|
56
|
-
* provides and then reads — `McpHarness.make(server.pipe(Layer.provide(AppLayer)))`
|
|
57
|
-
* under `Effect.provide(AppLayer)` builds `AppLayer` twice, so the tools write
|
|
58
|
-
* to one instance and the test reads the other. Leave the service in the
|
|
59
|
-
* server layer's requirements and provide it once from the test, or build it
|
|
60
|
-
* once and pass `Layer.succeed(Tag, value)`.
|
|
61
|
-
*
|
|
62
|
-
* - Responses are matched by id, so notifications may interleave freely.
|
|
63
|
-
* - Every response wait and `awaitOutboundMethod` fails with `ServerStopped`
|
|
64
|
-
* when the server stops, and dies when `strictStdout` sees a line that is
|
|
65
|
-
* not JSON-RPC. `stop` stops the server at once; `close` ends stdin and lets
|
|
66
|
-
* in-flight requests answer first, so only `stop` releases a wait on a
|
|
67
|
-
* request that never completes.
|
|
68
|
-
* - With `captureLogs`, the server's console is captured: `stderrSoFar`
|
|
69
|
-
* holds stderr writes and every log line, and `consoleLogSoFar` holds
|
|
70
|
-
* anything that went through `console.log`, which in a real server is
|
|
71
|
-
* the wire. Assert it empty.
|
|
72
|
-
* - On a stateful revision (the default `2025-11-25`), send `initialize`
|
|
73
|
-
* first. `ping` is the exception: the server answers it `{}` before
|
|
74
|
-
* initialization, so it is written. Every other request before
|
|
75
|
-
* `initialize` fails with `NotInitialized` and is never written: the
|
|
76
|
-
* server would only answer an opaque refusal,
|
|
77
|
-
* `-32602 Invalid request metadata` when a stateless adapter is listed
|
|
78
|
-
* first (as in `McpStdio.protocols`), or `-32603 Internal error` when
|
|
79
|
-
* only stateful revisions are served.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* import { McpHarness } from "@effected/mcp/testing";
|
|
84
|
-
* import { Effect } from "effect";
|
|
85
|
-
*
|
|
86
|
-
* // `ServerLayer` is the server's layer, built without its own `Stdio`.
|
|
87
|
-
* const program = Effect.gen(function* () {
|
|
88
|
-
* const harness = yield* McpHarness.make(ServerLayer);
|
|
89
|
-
* yield* harness.initialize;
|
|
90
|
-
* const tools = yield* harness.listTools;
|
|
91
|
-
* return tools.map((tool) => tool.name);
|
|
92
|
-
* }).pipe(Effect.scoped);
|
|
93
|
-
* ```
|
|
94
|
-
*
|
|
95
|
-
* @public
|
|
96
|
-
*/
|
|
97
|
-
var McpHarness = class McpHarness {
|
|
98
|
-
/** The revision this client speaks. */
|
|
99
|
-
protocol;
|
|
100
|
-
/** `initialize` then `notifications/initialized`; `server/discover` on a stateless revision. Call it first on a stateful revision. */
|
|
101
|
-
initialize;
|
|
102
|
-
/**
|
|
103
|
-
* `initialize` asking for `protocolVersion` instead of the harness's own
|
|
104
|
-
* revision, then `notifications/initialized` only when the response
|
|
105
|
-
* carries no error. Returns the whole response: assert the negotiated
|
|
106
|
-
* `result.protocolVersion`, or the JSON-RPC `error`.
|
|
107
|
-
*
|
|
108
|
-
* @remarks
|
|
109
|
-
* It moves between stateful revisions only. A stateful server never
|
|
110
|
-
* refuses a version: it counter-offers its latest stateful revision, so
|
|
111
|
-
* asking for an unknown one, or for the stateless `2026-07-28`, comes back
|
|
112
|
-
* as `2025-11-25`. On a harness made with a stateless revision the server
|
|
113
|
-
* does not serve `initialize` at all, and the response is a JSON-RPC
|
|
114
|
-
* `-32601` "Method not found" error, with no `notifications/initialized`
|
|
115
|
-
* sent after it.
|
|
116
|
-
*/
|
|
117
|
-
initializeWith;
|
|
118
|
-
/** `server/discover`. */
|
|
119
|
-
discover;
|
|
120
|
-
/** Send a request and wait for its response. */
|
|
121
|
-
request;
|
|
122
|
-
/** Send a request now; wait for its response later. Fails with `NotInitialized`, sending nothing, on a stateful revision before `initialize` (`ping` excepted). */
|
|
123
|
-
startRequest;
|
|
124
|
-
/** Send a notification. */
|
|
125
|
-
notify;
|
|
126
|
-
/** `tools/call`, returning the whole response: a JSON-RPC error is data here. */
|
|
127
|
-
callTool;
|
|
128
|
-
/** `tools/list`'s tools; a JSON-RPC error fails with `ErrorResponse`. */
|
|
129
|
-
listTools;
|
|
130
|
-
/** `resources/list`'s resources; a JSON-RPC error fails with `ErrorResponse`. */
|
|
131
|
-
listResources;
|
|
132
|
-
/** `resources/read`, returning the whole response. */
|
|
133
|
-
readResource;
|
|
134
|
-
/** Write any value as one newline-framed line. Never gated; a raw `initialize` counts as sent. */
|
|
135
|
-
sendRaw;
|
|
136
|
-
/** The next server-initiated frame with this method, skipping and keeping others; fails with `ServerStopped` once the server stops. */
|
|
137
|
-
awaitOutboundMethod;
|
|
138
|
-
/** Every frame written to the server's stdin so far, in order, as the JSON-parsed bytes written (a snapshot the caller's later mutations cannot reach): requests, notifications and `sendRaw` values. */
|
|
139
|
-
sentSoFar;
|
|
140
|
-
/** Everything written to stderr, plus every captured log line. */
|
|
141
|
-
stderrSoFar;
|
|
142
|
-
/**
|
|
143
|
-
* Every captured `console.log`, `info` or `debug` call, its arguments joined with a space, each rendered close to
|
|
144
|
-
* what the console would show: a string as itself, a value with its own string form as that form (an `Error` as
|
|
145
|
-
* `Error: boom`, a number, a date), an array or a plain object as JSON (`{"message":"x"}`, not just `x`).
|
|
146
|
-
*
|
|
147
|
-
* @remarks
|
|
148
|
-
* Rendering never throws inside the server's own call. A value whose string form throws (an object with no
|
|
149
|
-
* prototype, a throwing `toString`) and has no JSON falls back to `ErrorText.describe` from `@effected/engine`.
|
|
150
|
-
*/
|
|
151
|
-
consoleLogSoFar;
|
|
152
|
-
/**
|
|
153
|
-
* End stdin, as a client disconnecting does. The server drains first:
|
|
154
|
-
* requests already in flight still answer, and the server stops once they
|
|
155
|
-
* have, so a wait on a request that never completes keeps waiting. Use
|
|
156
|
-
* {@link McpHarness.stop} to end the server regardless.
|
|
157
|
-
*/
|
|
158
|
-
close;
|
|
159
|
-
/**
|
|
160
|
-
* Interrupt the server without draining, as a client killing the process
|
|
161
|
-
* does: every pending response wait and `awaitOutboundMethod` fails with
|
|
162
|
-
* `ServerStopped`.
|
|
163
|
-
*/
|
|
164
|
-
stop;
|
|
165
|
-
constructor(parts) {
|
|
166
|
-
this.protocol = parts.protocol;
|
|
167
|
-
this.initialize = parts.initialize;
|
|
168
|
-
this.initializeWith = parts.initializeWith;
|
|
169
|
-
this.discover = parts.discover;
|
|
170
|
-
this.request = parts.request;
|
|
171
|
-
this.startRequest = parts.startRequest;
|
|
172
|
-
this.notify = parts.notify;
|
|
173
|
-
this.callTool = parts.callTool;
|
|
174
|
-
this.listTools = parts.listTools;
|
|
175
|
-
this.listResources = parts.listResources;
|
|
176
|
-
this.readResource = parts.readResource;
|
|
177
|
-
this.sendRaw = parts.sendRaw;
|
|
178
|
-
this.awaitOutboundMethod = parts.awaitOutboundMethod;
|
|
179
|
-
this.sentSoFar = parts.sentSoFar;
|
|
180
|
-
this.stderrSoFar = parts.stderrSoFar;
|
|
181
|
-
this.consoleLogSoFar = parts.consoleLogSoFar;
|
|
182
|
-
this.close = parts.close;
|
|
183
|
-
this.stop = parts.stop;
|
|
184
|
-
}
|
|
185
|
-
/** Build `server` over queue-backed stdio and return a client for it; fails with the layer's own error if it cannot build. */
|
|
186
|
-
static make = (server, options = {}) => Effect.gen(function* () {
|
|
187
|
-
const protocol = options.protocol ?? McpProtocol.v2025_11_25;
|
|
188
|
-
const clientInfo = options.clientInfo ?? DEFAULT_CLIENT_INFO;
|
|
189
|
-
const strictStdout = options.strictStdout ?? true;
|
|
190
|
-
const stdin = yield* Queue.unbounded();
|
|
191
|
-
const stdout = yield* Queue.unbounded();
|
|
192
|
-
const inbound = yield* Queue.unbounded();
|
|
193
|
-
const retained = [];
|
|
194
|
-
const waiters = /* @__PURE__ */ new Map();
|
|
195
|
-
const stderr = [];
|
|
196
|
-
const consoleLog = [];
|
|
197
|
-
const ready = yield* Deferred.make();
|
|
198
|
-
const stopped = yield* Deferred.make();
|
|
199
|
-
const corrupt = yield* Deferred.make();
|
|
200
|
-
const encoder = new TextEncoder();
|
|
201
|
-
const stdoutDecoder = new TextDecoder();
|
|
202
|
-
const stderrDecoder = new TextDecoder();
|
|
203
|
-
let nextId = 1;
|
|
204
|
-
const stdio = Stdio.layerTest({
|
|
205
|
-
stdin: Stream.fromQueue(stdin),
|
|
206
|
-
stdout: () => Sink.forEach((chunk) => Queue.offer(stdout, chunk)),
|
|
207
|
-
stderr: () => Sink.forEach((chunk) => Effect.sync(() => {
|
|
208
|
-
stderr.push(typeof chunk === "string" ? chunk : stderrDecoder.decode(chunk, { stream: true }));
|
|
209
|
-
}))
|
|
210
|
-
});
|
|
211
|
-
const ambient = yield* Console.Console;
|
|
212
|
-
const show = (args) => args.map(showArg).join(" ");
|
|
213
|
-
const toStdout = (...args) => {
|
|
214
|
-
consoleLog.push(show(args));
|
|
215
|
-
};
|
|
216
|
-
const toStderr = (...args) => {
|
|
217
|
-
stderr.push(`${show(args)}\n`);
|
|
218
|
-
};
|
|
219
|
-
const captured = {
|
|
220
|
-
...ambient,
|
|
221
|
-
log: toStdout,
|
|
222
|
-
info: toStdout,
|
|
223
|
-
debug: toStdout,
|
|
224
|
-
error: toStderr,
|
|
225
|
-
warn: toStderr
|
|
226
|
-
};
|
|
227
|
-
const provided = options.captureLogs === false ? server.pipe(Layer.provide(stdio)) : server.pipe(Layer.provide(stdio), Layer.provide(Layer.succeed(Console.Console, captured)));
|
|
228
|
-
const route = (line) => {
|
|
229
|
-
const parsed = parseFrame(line);
|
|
230
|
-
if (!isJsonRpcMessage(parsed)) return strictStdout ? Effect.asVoid(Deferred.die(corrupt, /* @__PURE__ */ new Error(`stdout carried a line that is not JSON-RPC: ${ToolFailure.truncate(line)}`))) : Effect.void;
|
|
231
|
-
if (isResponse(parsed)) {
|
|
232
|
-
const key = requestKey(parsed.id);
|
|
233
|
-
const waiter = waiters.get(key);
|
|
234
|
-
if (waiter !== void 0) {
|
|
235
|
-
waiters.delete(key);
|
|
236
|
-
return Effect.asVoid(Deferred.succeed(waiter, parsed));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return Effect.asVoid(Queue.offer(inbound, parsed));
|
|
240
|
-
};
|
|
241
|
-
const router = yield* Stream.fromQueue(stdout).pipe(Stream.map((chunk) => typeof chunk === "string" ? chunk : stdoutDecoder.decode(chunk, { stream: true })), Stream.splitLines, Stream.runForEach((line) => line.length === 0 ? Effect.void : route(line)), Effect.forkScoped);
|
|
242
|
-
const serverFiber = yield* Effect.flatMap(Effect.scope, (scope) => Layer.buildWithMemoMap(provided, Layer.makeMemoMapUnsafe(), scope)).pipe(Effect.andThen(Deferred.succeed(ready, void 0)), Effect.andThen(Effect.never), Effect.scoped, Effect.onExit((exit) => Effect.andThen(Exit.isFailure(exit) ? Deferred.failCause(ready, exit.cause) : Effect.void, Queue.end(stdout).pipe(Effect.andThen(Fiber.await(router)), Effect.andThen(Deferred.fail(stopped, new McpTestFailure({
|
|
243
|
-
reason: "ServerStopped",
|
|
244
|
-
message: "the server stopped before it responded"
|
|
245
|
-
})))))), Effect.forkScoped);
|
|
246
|
-
yield* Deferred.await(ready);
|
|
247
|
-
const stopAware = (wait) => Effect.raceAllFirst([
|
|
248
|
-
wait,
|
|
249
|
-
Deferred.await(stopped),
|
|
250
|
-
Deferred.await(corrupt)
|
|
251
|
-
]);
|
|
252
|
-
const awaitResponse = (waiter) => Effect.suspend(() => Deferred.isDoneUnsafe(waiter) ? Deferred.await(waiter) : stopAware(Deferred.await(waiter)));
|
|
253
|
-
const stateful = !isStateless(protocol);
|
|
254
|
-
let initializeSent = false;
|
|
255
|
-
const sent = [];
|
|
256
|
-
const sendRaw = (message) => Effect.suspend(() => {
|
|
257
|
-
const line = JSON.stringify(message);
|
|
258
|
-
const written = line === void 0 ? void 0 : JSON.parse(line);
|
|
259
|
-
sent.push(written);
|
|
260
|
-
if (isJsonRpcMessage(written) && written.method === "initialize") initializeSent = true;
|
|
261
|
-
return Effect.asVoid(Queue.offer(stdin, encoder.encode(`${line}\n`)));
|
|
262
|
-
});
|
|
263
|
-
const startRequest = (method, params) => Effect.gen(function* () {
|
|
264
|
-
if (stateful && !initializeSent && method !== "initialize" && method !== "ping") return yield* new McpTestFailure({
|
|
265
|
-
reason: "NotInitialized",
|
|
266
|
-
message: `${method} was not sent: call initialize first on stateful protocol ${protocol.protocolVersion}`
|
|
267
|
-
});
|
|
268
|
-
const id = nextId++;
|
|
269
|
-
const waiter = yield* Deferred.make();
|
|
270
|
-
waiters.set(requestKey(id), waiter);
|
|
271
|
-
yield* sendRaw(frame(protocol, clientInfo, method, params, id));
|
|
272
|
-
return {
|
|
273
|
-
id,
|
|
274
|
-
response: awaitResponse(waiter)
|
|
275
|
-
};
|
|
276
|
-
});
|
|
277
|
-
const request = (method, params) => Effect.flatMap(startRequest(method, params), (started) => started.response);
|
|
278
|
-
const notify = (method, params) => sendRaw(frame(protocol, clientInfo, method, params));
|
|
279
|
-
const discover = request("server/discover", {});
|
|
280
|
-
const initialize = isStateless(protocol) ? discover : Effect.gen(function* () {
|
|
281
|
-
const response = yield* request("initialize", initializeParams(protocol, clientInfo));
|
|
282
|
-
yield* notify("notifications/initialized");
|
|
283
|
-
return response;
|
|
284
|
-
});
|
|
285
|
-
const initializeWith = (protocolVersion) => Effect.gen(function* () {
|
|
286
|
-
const response = yield* request("initialize", {
|
|
287
|
-
...initializeParams(protocol, clientInfo),
|
|
288
|
-
protocolVersion
|
|
289
|
-
});
|
|
290
|
-
if (response.error === void 0) yield* notify("notifications/initialized");
|
|
291
|
-
return response;
|
|
292
|
-
});
|
|
293
|
-
const listTools = Effect.flatMap(request("tools/list"), (response) => response.error === void 0 ? Effect.succeed(response.result.tools) : Effect.fail(new McpTestFailure({
|
|
294
|
-
reason: "ErrorResponse",
|
|
295
|
-
message: `tools/list failed: ${ToolFailure.truncate(JSON.stringify(response.error))}`
|
|
296
|
-
})));
|
|
297
|
-
const listResources = Effect.flatMap(request("resources/list"), (response) => response.error === void 0 ? Effect.succeed(response.result.resources) : Effect.fail(new McpTestFailure({
|
|
298
|
-
reason: "ErrorResponse",
|
|
299
|
-
message: `resources/list failed: ${ToolFailure.truncate(JSON.stringify(response.error))}`
|
|
300
|
-
})));
|
|
301
|
-
const awaitOutboundMethod = (method) => Effect.suspend(() => {
|
|
302
|
-
const index = retained.findIndex((message) => message.method === method);
|
|
303
|
-
if (index !== -1) return Effect.succeed(retained.splice(index, 1)[0]);
|
|
304
|
-
return stopAware(Effect.gen(function* () {
|
|
305
|
-
while (true) {
|
|
306
|
-
const message = yield* Queue.take(inbound);
|
|
307
|
-
if (message.method === method) return message;
|
|
308
|
-
retained.push(message);
|
|
309
|
-
}
|
|
310
|
-
}));
|
|
311
|
-
});
|
|
312
|
-
return new McpHarness({
|
|
313
|
-
protocol,
|
|
314
|
-
initialize,
|
|
315
|
-
initializeWith,
|
|
316
|
-
discover,
|
|
317
|
-
request,
|
|
318
|
-
startRequest,
|
|
319
|
-
notify,
|
|
320
|
-
callTool: (name, args) => request("tools/call", {
|
|
321
|
-
name,
|
|
322
|
-
arguments: args ?? {}
|
|
323
|
-
}),
|
|
324
|
-
listTools,
|
|
325
|
-
listResources,
|
|
326
|
-
readResource: (uri) => request("resources/read", { uri }),
|
|
327
|
-
sendRaw,
|
|
328
|
-
awaitOutboundMethod,
|
|
329
|
-
sentSoFar: Effect.sync(() => [...sent]),
|
|
330
|
-
stderrSoFar: Effect.sync(() => stderr.join("")),
|
|
331
|
-
consoleLogSoFar: Effect.sync(() => [...consoleLog]),
|
|
332
|
-
close: Effect.asVoid(Queue.end(stdin)),
|
|
333
|
-
stop: Fiber.interrupt(serverFiber)
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
export { McpHarness };
|