@dxos/mcp-client 0.8.3

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/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ MIT License
2
+ Copyright (c) 2022 DXOS
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # @dxos/mcp-client
2
+
3
+ MCP client.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm i @dxos/mcp-client
9
+ ```
10
+
11
+ ## DXOS Resources
12
+
13
+ - [Website](https://dxos.org)
14
+ - [Developer Documentation](https://docs.dxos.org)
15
+ - Talk to us on [Discord](https://dxos.org/discord)
16
+
17
+ ## Contributions
18
+
19
+ Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
20
+
21
+ License: [MIT](./LICENSE) Copyright 2022 © DXOS
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@dxos/mcp-client",
3
+ "version": "0.8.3",
4
+ "description": "MCP client.",
5
+ "homepage": "https://dxos.org",
6
+ "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
11
+ "license": "MIT",
12
+ "author": "info@dxos.org",
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "source": "./src/index.ts",
17
+ "types": "./dist/types/src/index.d.ts",
18
+ "default": "./dist/lib/neutral/index.mjs"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "src"
24
+ ],
25
+ "dependencies": {
26
+ "@effect/ai": "0.33.2",
27
+ "@modelcontextprotocol/sdk": "^1.27.1",
28
+ "effect": "3.19.16",
29
+ "@dxos/ai": "0.8.3",
30
+ "@dxos/effect": "0.8.3",
31
+ "@dxos/invariant": "0.8.3",
32
+ "@dxos/log": "0.8.3"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "beast": {}
38
+ }
@@ -0,0 +1,123 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import * as Chat from '@effect/ai/Chat';
6
+ import type * as LanguageModel from '@effect/ai/LanguageModel';
7
+ import * as Prompt from '@effect/ai/Prompt';
8
+ import { describe, it } from '@effect/vitest';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as Layer from 'effect/Layer';
11
+ import * as Schema from 'effect/Schema';
12
+
13
+ import { AiService, type GenericToolkit } from '@dxos/ai';
14
+ import { TestAiService } from '@dxos/ai/testing';
15
+ import { TestHelpers } from '@dxos/effect/testing';
16
+ import { log } from '@dxos/log';
17
+
18
+ import * as McpToolkit from './McpToolkit';
19
+
20
+ const AiServiceLayer = AiService.model('@anthropic/claude-opus-4-6', { thinking: false }).pipe(
21
+ Layer.provide(
22
+ TestAiService({
23
+ disableMemoization: true,
24
+ }),
25
+ ),
26
+ );
27
+
28
+ describe('Browser Automation', () => {
29
+ it.effect(
30
+ 'smoke',
31
+ Effect.fnUntraced(
32
+ function* (_) {
33
+ const browserAutomationToolkit = yield* McpToolkit.make({
34
+ url: 'https://playwright-mcp-example.dxos.workers.dev/sse',
35
+ kind: 'sse',
36
+ });
37
+
38
+ const chat = yield* Chat.empty;
39
+ let prompt: Prompt.RawInput = 'Check that you are able to use the browser. Open https://example.com';
40
+ let output: LanguageModel.GenerateTextResponse<GenericToolkit.GenericTools>;
41
+
42
+ do {
43
+ output = yield* chat
44
+ .generateText({
45
+ prompt,
46
+ toolkit: browserAutomationToolkit.toolkit,
47
+ })
48
+ .pipe(Effect.provide(browserAutomationToolkit.layer));
49
+ log.info('results', { text: output.text, toolCalls: output.toolCalls.map((_) => _.name) });
50
+ prompt = Prompt.empty;
51
+ } while (
52
+ // Agentic loop.
53
+ // TODO(burdon): Explain how this works?
54
+ output.toolCalls.length > 0
55
+ );
56
+ log.info('chat', { chat: yield* chat.export });
57
+ },
58
+ Effect.provide(AiServiceLayer),
59
+ TestHelpers.provideTestContext,
60
+ TestHelpers.taggedTest('llm'),
61
+ ),
62
+ { timeout: 120_000 },
63
+ );
64
+
65
+ it.effect(
66
+ 'effect-blog',
67
+ Effect.fnUntraced(
68
+ function* (_) {
69
+ const browserAutomationToolkit = yield* McpToolkit.make({
70
+ url: 'https://playwright-mcp-example.dxos.workers.dev/sse',
71
+ kind: 'sse',
72
+ });
73
+
74
+ const chat = yield* Chat.empty;
75
+ let prompt: Prompt.RawInput =
76
+ '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<GenericToolkit.GenericTools>;
78
+
79
+ do {
80
+ output = yield* chat
81
+ .generateText({
82
+ prompt,
83
+ toolkit: browserAutomationToolkit.toolkit,
84
+ })
85
+ .pipe(Effect.provide(browserAutomationToolkit.layer));
86
+ log.info('results', { text: output.text, toolCalls: output.toolCalls.map((_) => _.name) });
87
+ prompt = Prompt.empty;
88
+ } while (
89
+ // Agentic loop.
90
+ // TODO(burdon): Explain how this works?
91
+ output.toolCalls.length > 0
92
+ );
93
+ log.info('chat', { chat: yield* chat.export });
94
+
95
+ const result = yield* chat.generateObject({
96
+ prompt: 'Extract blog articles and people associated with them.',
97
+ schema: Schema.Struct({
98
+ authors: Schema.Array(
99
+ Schema.Struct({
100
+ name: Schema.String,
101
+ position: Schema.String,
102
+ url: Schema.String,
103
+ }),
104
+ ),
105
+ articles: Schema.Array(
106
+ Schema.Struct({
107
+ heading: Schema.String,
108
+ content: Schema.String,
109
+ url: Schema.String,
110
+ }),
111
+ ),
112
+ }),
113
+ });
114
+
115
+ log.info('result', { result });
116
+ },
117
+ Effect.provide(AiServiceLayer),
118
+ TestHelpers.provideTestContext,
119
+ TestHelpers.taggedTest('llm'),
120
+ ),
121
+ { timeout: 120_000 },
122
+ );
123
+ });
@@ -0,0 +1,129 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import * as Tool from '@effect/ai/Tool';
6
+ import * as Toolkit from '@effect/ai/Toolkit';
7
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
8
+ import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
9
+ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
10
+ import * as Effect from 'effect/Effect';
11
+ import * as Schema from 'effect/Schema';
12
+
13
+ import { GenericToolkit } from '@dxos/ai';
14
+ import { invariant } from '@dxos/invariant';
15
+
16
+ export interface McpToolkitOptions {
17
+ url: string;
18
+ kind: 'sse' | 'http';
19
+ }
20
+
21
+ /**
22
+ * Creates a GenericToolkit that connects to an MCP server and exposes its tools to the assistant.
23
+ *
24
+ * @param options MCP server URL and transport kind ('sse' for SSE, 'http' for Streamable HTTP).
25
+ * @returns A GenericToolkit containing all tools from the MCP server.
26
+ */
27
+ export const make = (options: McpToolkitOptions): Effect.Effect<GenericToolkit.GenericToolkit> =>
28
+ Effect.gen(function* () {
29
+ const transport = createTransport(options.url, options.kind);
30
+ const client = new Client({ name: '@dxos/mcp-client', version: '0.8.3' });
31
+ yield* Effect.promise(() => client.connect(transport));
32
+
33
+ const { tools } = yield* Effect.promise(() => client.listTools());
34
+
35
+ if (tools.length === 0) {
36
+ return GenericToolkit.empty;
37
+ }
38
+
39
+ const effectTools = tools.map((mcpTool) => {
40
+ const parameters: any = {};
41
+ for (const [key, value] of Object.entries(mcpTool.inputSchema.properties ?? {})) {
42
+ if (mcpTool.inputSchema.required?.includes(key)) {
43
+ parameters[key] = Schema.Unknown.pipe(
44
+ Schema.annotations({
45
+ jsonSchema: value,
46
+ }),
47
+ );
48
+ } else {
49
+ parameters[key] = Schema.Unknown.pipe(
50
+ Schema.annotations({
51
+ jsonSchema: value,
52
+ }),
53
+ ).pipe(Schema.optional);
54
+ }
55
+ }
56
+
57
+ return Tool.make(sanitizeToolName(mcpTool.name), {
58
+ description: mcpTool.description ?? `MCP tool: ${mcpTool.name}`,
59
+ parameters,
60
+ success: Schema.String,
61
+ failure: Schema.Never,
62
+ });
63
+ });
64
+
65
+ const toolkit = Toolkit.make(...effectTools);
66
+
67
+ const handlers = Object.fromEntries(
68
+ tools.map((mcpTool) => {
69
+ const toolName = sanitizeToolName(mcpTool.name);
70
+ return [
71
+ toolName,
72
+ Effect.fn(function* (args: unknown) {
73
+ const result = yield* Effect.promise(() =>
74
+ client.callTool({
75
+ name: mcpTool.name,
76
+ arguments: args as any,
77
+ }),
78
+ );
79
+ return formatToolResult(result);
80
+ }),
81
+ ];
82
+ }),
83
+ ) as any;
84
+
85
+ const layer = toolkit.toLayer(handlers);
86
+
87
+ return GenericToolkit.make(toolkit, layer);
88
+ }).pipe(Effect.withSpan('McpToolkit.make'));
89
+
90
+ /**
91
+ * Creates a transport for the given MCP server URL and kind.
92
+ */
93
+ const createTransport = (
94
+ url: string,
95
+ kind: McpToolkitOptions['kind'],
96
+ ): SSEClientTransport | StreamableHTTPClientTransport => {
97
+ const urlObj = new URL(url);
98
+ switch (kind) {
99
+ case 'sse':
100
+ return new SSEClientTransport(urlObj);
101
+ case 'http':
102
+ return new StreamableHTTPClientTransport(urlObj);
103
+ default: {
104
+ const _exhaustive: never = kind;
105
+ return invariant(false, `Unsupported MCP transport kind: ${_exhaustive}`) as never;
106
+ }
107
+ }
108
+ };
109
+
110
+ /**
111
+ * Sanitizes MCP tool names for use as Effect tool names (replaces / with _).
112
+ */
113
+ const sanitizeToolName = (name: string): string => name.replace(/\//g, '_');
114
+
115
+ /**
116
+ * Formats MCP callTool result content for the assistant.
117
+ */
118
+ const formatToolResult = (result: Awaited<ReturnType<Client['callTool']>>): string => {
119
+ if ('toolResult' in result && result.toolResult !== undefined) {
120
+ return JSON.stringify(result.toolResult);
121
+ }
122
+ if ('content' in result && Array.isArray(result.content)) {
123
+ return result.content
124
+ .filter((part): part is { type: 'text'; text: string } => part.type === 'text')
125
+ .map((part) => part.text)
126
+ .join('\n');
127
+ }
128
+ return String(result);
129
+ };
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export * as McpToolkit from './McpToolkit';