@dxos/mcp-client 0.8.3 → 0.8.4-main.03d5cd7b56
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/lib/neutral/index.mjs +168 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/McpToolkit.d.ts +29 -0
- package/dist/types/src/McpToolkit.d.ts.map +1 -0
- package/dist/types/src/McpToolkit.test.d.ts +2 -0
- package/dist/types/src/McpToolkit.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/is405.test.d.ts +2 -0
- package/dist/types/src/is405.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +6 -6
- package/src/McpToolkit.test.ts +34 -9
- package/src/McpToolkit.ts +127 -33
- package/src/is405.test.ts +33 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/mcp-client",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4-main.03d5cd7b56",
|
|
4
4
|
"description": "MCP client.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@effect/ai": "0.33.2",
|
|
27
27
|
"@modelcontextprotocol/sdk": "^1.27.1",
|
|
28
|
-
"effect": "3.
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/effect": "0.8.
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
28
|
+
"effect": "3.20.0",
|
|
29
|
+
"@dxos/invariant": "0.8.4-main.03d5cd7b56",
|
|
30
|
+
"@dxos/effect": "0.8.4-main.03d5cd7b56",
|
|
31
|
+
"@dxos/log": "0.8.4-main.03d5cd7b56",
|
|
32
|
+
"@dxos/ai": "0.8.4-main.03d5cd7b56"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
package/src/McpToolkit.test.ts
CHANGED
|
@@ -10,8 +10,9 @@ import * as Effect from 'effect/Effect';
|
|
|
10
10
|
import * as Layer from 'effect/Layer';
|
|
11
11
|
import * as Schema from 'effect/Schema';
|
|
12
12
|
|
|
13
|
-
import { AiService, type
|
|
13
|
+
import { AiService, type OpaqueToolkit } from '@dxos/ai';
|
|
14
14
|
import { TestAiService } from '@dxos/ai/testing';
|
|
15
|
+
import { runAndForwardErrors } from '@dxos/effect';
|
|
15
16
|
import { TestHelpers } from '@dxos/effect/testing';
|
|
16
17
|
import { log } from '@dxos/log';
|
|
17
18
|
|
|
@@ -25,6 +26,26 @@ const AiServiceLayer = AiService.model('@anthropic/claude-opus-4-6', { thinking:
|
|
|
25
26
|
),
|
|
26
27
|
);
|
|
27
28
|
|
|
29
|
+
describe('connectWithFallback', () => {
|
|
30
|
+
it(
|
|
31
|
+
'connects to Linear MCP (SSE kind falls back to HTTP)',
|
|
32
|
+
{
|
|
33
|
+
skip: !process.env.LINEAR_API_KEY,
|
|
34
|
+
timeout: 30_000,
|
|
35
|
+
},
|
|
36
|
+
async () => {
|
|
37
|
+
const toolkit = await runAndForwardErrors(
|
|
38
|
+
McpToolkit.make({
|
|
39
|
+
url: 'https://mcp.linear.app/mcp',
|
|
40
|
+
protocol: 'sse',
|
|
41
|
+
apiKey: process.env.LINEAR_API_KEY,
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
log.info('connected', { tools: Object.keys(toolkit.toolkit.tools) });
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
28
49
|
describe('Browser Automation', () => {
|
|
29
50
|
it.effect(
|
|
30
51
|
'smoke',
|
|
@@ -32,12 +53,12 @@ describe('Browser Automation', () => {
|
|
|
32
53
|
function* (_) {
|
|
33
54
|
const browserAutomationToolkit = yield* McpToolkit.make({
|
|
34
55
|
url: 'https://playwright-mcp-example.dxos.workers.dev/sse',
|
|
35
|
-
|
|
56
|
+
protocol: 'sse',
|
|
36
57
|
});
|
|
37
58
|
|
|
38
59
|
const chat = yield* Chat.empty;
|
|
39
60
|
let prompt: Prompt.RawInput = 'Check that you are able to use the browser. Open https://example.com';
|
|
40
|
-
let output: LanguageModel.GenerateTextResponse<
|
|
61
|
+
let output: LanguageModel.GenerateTextResponse<OpaqueToolkit.OpaqueTools>;
|
|
41
62
|
|
|
42
63
|
do {
|
|
43
64
|
output = yield* chat
|
|
@@ -57,9 +78,11 @@ describe('Browser Automation', () => {
|
|
|
57
78
|
},
|
|
58
79
|
Effect.provide(AiServiceLayer),
|
|
59
80
|
TestHelpers.provideTestContext,
|
|
60
|
-
TestHelpers.taggedTest('llm'),
|
|
61
81
|
),
|
|
62
|
-
{
|
|
82
|
+
{
|
|
83
|
+
timeout: 120_000,
|
|
84
|
+
tags: ['llm'],
|
|
85
|
+
},
|
|
63
86
|
);
|
|
64
87
|
|
|
65
88
|
it.effect(
|
|
@@ -68,13 +91,13 @@ describe('Browser Automation', () => {
|
|
|
68
91
|
function* (_) {
|
|
69
92
|
const browserAutomationToolkit = yield* McpToolkit.make({
|
|
70
93
|
url: 'https://playwright-mcp-example.dxos.workers.dev/sse',
|
|
71
|
-
|
|
94
|
+
protocol: 'sse',
|
|
72
95
|
});
|
|
73
96
|
|
|
74
97
|
const chat = yield* Chat.empty;
|
|
75
98
|
let prompt: Prompt.RawInput =
|
|
76
99
|
'Scrape effect blog at https://effect.website/blog and find the content of last 3 articles. Next prompt I will ask you generate structured representation.';
|
|
77
|
-
let output: LanguageModel.GenerateTextResponse<
|
|
100
|
+
let output: LanguageModel.GenerateTextResponse<OpaqueToolkit.OpaqueTools>;
|
|
78
101
|
|
|
79
102
|
do {
|
|
80
103
|
output = yield* chat
|
|
@@ -116,8 +139,10 @@ describe('Browser Automation', () => {
|
|
|
116
139
|
},
|
|
117
140
|
Effect.provide(AiServiceLayer),
|
|
118
141
|
TestHelpers.provideTestContext,
|
|
119
|
-
TestHelpers.taggedTest('llm'),
|
|
120
142
|
),
|
|
121
|
-
{
|
|
143
|
+
{
|
|
144
|
+
timeout: 120_000,
|
|
145
|
+
tags: ['llm'],
|
|
146
|
+
},
|
|
122
147
|
);
|
|
123
148
|
});
|
package/src/McpToolkit.ts
CHANGED
|
@@ -2,55 +2,75 @@
|
|
|
2
2
|
// Copyright 2026 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
5
7
|
import * as Tool from '@effect/ai/Tool';
|
|
6
8
|
import * as Toolkit from '@effect/ai/Toolkit';
|
|
7
9
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
10
|
+
// SSEClientTransport is marked @deprecated in the SDK in favor of StreamableHTTP, but the
|
|
11
|
+
// SDK itself notes that clients should keep supporting both while servers migrate.
|
|
12
|
+
// `connectWithFallback` below tries the configured protocol first, then the other on 405.
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
|
8
14
|
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
9
15
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
16
|
+
import * as Cause from 'effect/Cause';
|
|
10
17
|
import * as Effect from 'effect/Effect';
|
|
11
18
|
import * as Schema from 'effect/Schema';
|
|
12
19
|
|
|
13
|
-
import {
|
|
20
|
+
import { OpaqueToolkit } from '@dxos/ai';
|
|
14
21
|
import { invariant } from '@dxos/invariant';
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Typed failure raised when the MCP client cannot connect or list tools.
|
|
25
|
+
* Carries the underlying cause and the server URL for diagnostics.
|
|
26
|
+
*/
|
|
27
|
+
export class McpConnectionError extends Schema.TaggedError<McpConnectionError>('McpConnectionError')(
|
|
28
|
+
'McpConnectionError',
|
|
29
|
+
{
|
|
30
|
+
url: Schema.String,
|
|
31
|
+
protocol: Schema.Literal('sse', 'http'),
|
|
32
|
+
message: Schema.String,
|
|
33
|
+
},
|
|
34
|
+
) {}
|
|
20
35
|
|
|
21
36
|
/**
|
|
22
|
-
* Creates
|
|
37
|
+
* Creates an OpaqueToolkit that connects to an MCP server and exposes its tools to the assistant.
|
|
23
38
|
*
|
|
24
|
-
* @param options MCP server URL and transport
|
|
25
|
-
* @returns
|
|
39
|
+
* @param options MCP server URL and transport protocol ('http' for Streamable HTTP, 'sse' for SSE).
|
|
40
|
+
* @returns An OpaqueToolkit containing all tools from the MCP server.
|
|
26
41
|
*/
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
const CLIENT_INFO = { name: '@dxos/mcp-client', version: '0.8.3' };
|
|
43
|
+
|
|
44
|
+
export interface McpToolkitOptions {
|
|
45
|
+
url: string;
|
|
46
|
+
protocol: 'sse' | 'http';
|
|
47
|
+
apiKey?: string;
|
|
48
|
+
}
|
|
32
49
|
|
|
33
|
-
|
|
50
|
+
export const make = (options: McpToolkitOptions): Effect.Effect<OpaqueToolkit.OpaqueToolkit, McpConnectionError> =>
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
const { client, protocol } = yield* connectWithFallback(options);
|
|
34
53
|
|
|
54
|
+
const { tools } = yield* Effect.tryPromise({
|
|
55
|
+
try: () => client.listTools(),
|
|
56
|
+
catch: (cause) =>
|
|
57
|
+
new McpConnectionError({
|
|
58
|
+
url: options.url,
|
|
59
|
+
protocol,
|
|
60
|
+
message: `Failed to list MCP tools: ${formatCause(cause)}`,
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
35
63
|
if (tools.length === 0) {
|
|
36
|
-
return
|
|
64
|
+
return OpaqueToolkit.empty;
|
|
37
65
|
}
|
|
38
66
|
|
|
39
67
|
const effectTools = tools.map((mcpTool) => {
|
|
40
68
|
const parameters: any = {};
|
|
41
69
|
for (const [key, value] of Object.entries(mcpTool.inputSchema.properties ?? {})) {
|
|
42
70
|
if (mcpTool.inputSchema.required?.includes(key)) {
|
|
43
|
-
parameters[key] = Schema.Unknown.pipe(
|
|
44
|
-
Schema.annotations({
|
|
45
|
-
jsonSchema: value,
|
|
46
|
-
}),
|
|
47
|
-
);
|
|
71
|
+
parameters[key] = Schema.Unknown.pipe(Schema.annotations({ jsonSchema: value }));
|
|
48
72
|
} else {
|
|
49
|
-
parameters[key] = Schema.Unknown.pipe(
|
|
50
|
-
Schema.annotations({
|
|
51
|
-
jsonSchema: value,
|
|
52
|
-
}),
|
|
53
|
-
).pipe(Schema.optional);
|
|
73
|
+
parameters[key] = Schema.Unknown.pipe(Schema.annotations({ jsonSchema: value })).pipe(Schema.optional);
|
|
54
74
|
}
|
|
55
75
|
}
|
|
56
76
|
|
|
@@ -84,25 +104,98 @@ export const make = (options: McpToolkitOptions): Effect.Effect<GenericToolkit.G
|
|
|
84
104
|
|
|
85
105
|
const layer = toolkit.toLayer(handlers);
|
|
86
106
|
|
|
87
|
-
return
|
|
107
|
+
return OpaqueToolkit.make(toolkit, layer);
|
|
88
108
|
}).pipe(Effect.withSpan('McpToolkit.make'));
|
|
89
109
|
|
|
90
110
|
/**
|
|
91
|
-
*
|
|
111
|
+
* Returns true when the error (or its wrapped cause) contains a 405 status code.
|
|
112
|
+
* `Effect.tryPromise` wraps thrown errors in `UnknownException`, so we unwrap first.
|
|
113
|
+
*/
|
|
114
|
+
export const is405 = (error: unknown): boolean => {
|
|
115
|
+
const cause = error != null && typeof error === 'object' && 'error' in error ? (error as any).error : error;
|
|
116
|
+
return cause instanceof Error && cause.message.includes('405');
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Connects to an MCP server, falling back to the alternate transport on 405 errors.
|
|
121
|
+
* Per the MCP spec, a 405 indicates the server uses the other transport protocol.
|
|
122
|
+
* Returns the connected Client (a fresh instance is created for the fallback attempt).
|
|
123
|
+
*
|
|
124
|
+
* Failures are surfaced as typed `McpConnectionError` so callers can recover (e.g. drop
|
|
125
|
+
* the misconfigured server) without breaking the surrounding effect.
|
|
126
|
+
*/
|
|
127
|
+
const connectWithFallback = (
|
|
128
|
+
options: McpToolkitOptions,
|
|
129
|
+
): Effect.Effect<{ client: Client; protocol: McpToolkitOptions['protocol'] }, McpConnectionError> =>
|
|
130
|
+
Effect.gen(function* () {
|
|
131
|
+
const fallbackProtocol = options.protocol === 'sse' ? 'http' : 'sse';
|
|
132
|
+
const primary = yield* connectClient(options.url, options.protocol, options.apiKey).pipe(Effect.either);
|
|
133
|
+
if (primary._tag === 'Right') {
|
|
134
|
+
return { client: primary.right, protocol: options.protocol };
|
|
135
|
+
}
|
|
136
|
+
if (is405(primary.left)) {
|
|
137
|
+
const fallback = yield* connectClient(options.url, fallbackProtocol, options.apiKey).pipe(Effect.either);
|
|
138
|
+
if (fallback._tag === 'Right') {
|
|
139
|
+
return { client: fallback.right, protocol: fallbackProtocol };
|
|
140
|
+
}
|
|
141
|
+
return yield* Effect.fail(
|
|
142
|
+
new McpConnectionError({
|
|
143
|
+
url: options.url,
|
|
144
|
+
protocol: fallbackProtocol,
|
|
145
|
+
message: `Failed to connect via ${fallbackProtocol} after 405 fallback: ${formatCause(fallback.left)}`,
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return yield* Effect.fail(
|
|
150
|
+
new McpConnectionError({
|
|
151
|
+
url: options.url,
|
|
152
|
+
protocol: options.protocol,
|
|
153
|
+
message: `Failed to connect via ${options.protocol}: ${formatCause(primary.left)}`,
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const connectClient = (url: string, protocol: McpToolkitOptions['protocol'], apiKey?: string) =>
|
|
159
|
+
Effect.tryPromise(() => {
|
|
160
|
+
const client = new Client(CLIENT_INFO);
|
|
161
|
+
const transport = createTransport(url, protocol, apiKey);
|
|
162
|
+
return client.connect(transport).then(() => client);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Renders a thrown value to a short string for inclusion in error messages.
|
|
167
|
+
* `Effect.tryPromise` wraps thrown errors in `UnknownException`; unwrap when present.
|
|
168
|
+
*/
|
|
169
|
+
const formatCause = (error: unknown): string => {
|
|
170
|
+
const inner = error != null && typeof error === 'object' && 'error' in error ? (error as any).error : error;
|
|
171
|
+
if (inner instanceof Error) {
|
|
172
|
+
return inner.message;
|
|
173
|
+
}
|
|
174
|
+
if (Cause.isCause(error)) {
|
|
175
|
+
return Cause.pretty(error);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return String(inner);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Creates a transport for the given MCP server URL and protocol.
|
|
92
183
|
*/
|
|
93
184
|
const createTransport = (
|
|
94
185
|
url: string,
|
|
95
|
-
|
|
186
|
+
protocol: McpToolkitOptions['protocol'],
|
|
187
|
+
apiKey?: string,
|
|
96
188
|
): SSEClientTransport | StreamableHTTPClientTransport => {
|
|
97
189
|
const urlObj = new URL(url);
|
|
98
|
-
|
|
190
|
+
const requestInit: RequestInit | undefined = apiKey ? { headers: { Authorization: `Bearer ${apiKey}` } } : undefined;
|
|
191
|
+
switch (protocol) {
|
|
99
192
|
case 'sse':
|
|
100
|
-
return new SSEClientTransport(urlObj);
|
|
193
|
+
return new SSEClientTransport(urlObj, { requestInit });
|
|
101
194
|
case 'http':
|
|
102
|
-
return new StreamableHTTPClientTransport(urlObj);
|
|
195
|
+
return new StreamableHTTPClientTransport(urlObj, { requestInit });
|
|
103
196
|
default: {
|
|
104
|
-
const _exhaustive: never =
|
|
105
|
-
return invariant(false, `Unsupported MCP transport
|
|
197
|
+
const _exhaustive: never = protocol;
|
|
198
|
+
return invariant(false, `Unsupported MCP transport protocol: ${_exhaustive}`) as never;
|
|
106
199
|
}
|
|
107
200
|
}
|
|
108
201
|
};
|
|
@@ -125,5 +218,6 @@ const formatToolResult = (result: Awaited<ReturnType<Client['callTool']>>): stri
|
|
|
125
218
|
.map((part) => part.text)
|
|
126
219
|
.join('\n');
|
|
127
220
|
}
|
|
221
|
+
|
|
128
222
|
return String(result);
|
|
129
223
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Cause from 'effect/Cause';
|
|
6
|
+
import { describe, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import * as McpToolkit from './McpToolkit';
|
|
9
|
+
|
|
10
|
+
describe('is405', () => {
|
|
11
|
+
test('matches raw Error with 405 in message', ({ expect }) => {
|
|
12
|
+
expect(McpToolkit.is405(new Error('SSE error: Non-200 status code (405)'))).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('matches UnknownException wrapping a 405 Error', ({ expect }) => {
|
|
16
|
+
const wrapped = new Cause.UnknownException(new Error('SSE error: Non-200 status code (405)'));
|
|
17
|
+
expect(McpToolkit.is405(wrapped)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('does not match unrelated errors', ({ expect }) => {
|
|
21
|
+
expect(McpToolkit.is405(new Error('connection refused'))).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('does not match UnknownException wrapping a non-405 Error', ({ expect }) => {
|
|
25
|
+
const wrapped = new Cause.UnknownException(new Error('connection refused'));
|
|
26
|
+
expect(McpToolkit.is405(wrapped)).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('does not match null or undefined', ({ expect }) => {
|
|
30
|
+
expect(McpToolkit.is405(null)).toBe(false);
|
|
31
|
+
expect(McpToolkit.is405(undefined)).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
});
|