mcp-use 0.0.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/LICENSE +21 -0
- package/README.md +130 -0
- package/dist/index.js +10 -0
- package/dist/src/adapters/base.js +124 -0
- package/dist/src/adapters/index.js +2 -0
- package/dist/src/adapters/langchain_adapter.js +105 -0
- package/dist/src/agents/base.js +9 -0
- package/dist/src/agents/index.js +3 -0
- package/dist/src/agents/mcp_agent.js +324 -0
- package/dist/src/agents/prompts/system_prompt_builder.js +40 -0
- package/dist/src/agents/prompts/templates.js +39 -0
- package/dist/src/agents/server_manager.js +152 -0
- package/dist/src/client.js +124 -0
- package/dist/src/config.js +30 -0
- package/dist/src/connectors/base.js +129 -0
- package/dist/src/connectors/http.js +51 -0
- package/dist/src/connectors/index.js +4 -0
- package/dist/src/connectors/stdio.js +49 -0
- package/dist/src/connectors/websocket.js +151 -0
- package/dist/src/logging.js +59 -0
- package/dist/src/session.js +52 -0
- package/dist/src/task_managers/base.js +127 -0
- package/dist/src/task_managers/index.js +4 -0
- package/dist/src/task_managers/sse.js +44 -0
- package/dist/src/task_managers/stdio.js +52 -0
- package/dist/src/task_managers/websocket.js +67 -0
- package/package.json +75 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
2
|
+
import { logger } from '../logging.js';
|
3
|
+
import { ConnectionManager } from './base.js';
|
4
|
+
export class SseConnectionManager extends ConnectionManager {
|
5
|
+
url;
|
6
|
+
opts;
|
7
|
+
_transport = null;
|
8
|
+
/**
|
9
|
+
* Create an SSE connection manager.
|
10
|
+
*
|
11
|
+
* @param url The SSE endpoint URL.
|
12
|
+
* @param opts Optional transport options (auth, headers, etc.).
|
13
|
+
*/
|
14
|
+
constructor(url, opts) {
|
15
|
+
super();
|
16
|
+
this.url = typeof url === 'string' ? new URL(url) : url;
|
17
|
+
this.opts = opts;
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Spawn a new `SSEClientTransport` and start the connection.
|
21
|
+
*/
|
22
|
+
async establishConnection() {
|
23
|
+
this._transport = new SSEClientTransport(this.url, this.opts);
|
24
|
+
// await this._transport.start()
|
25
|
+
logger.debug(`${this.constructor.name} connected successfully`);
|
26
|
+
return this._transport;
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* Close the underlying transport and clean up resources.
|
30
|
+
*/
|
31
|
+
async closeConnection(_connection) {
|
32
|
+
if (this._transport) {
|
33
|
+
try {
|
34
|
+
await this._transport.close();
|
35
|
+
}
|
36
|
+
catch (e) {
|
37
|
+
logger.warn(`Error closing SSE transport: ${e}`);
|
38
|
+
}
|
39
|
+
finally {
|
40
|
+
this._transport = null;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
2
|
+
import { logger } from '../logging.js';
|
3
|
+
import { ConnectionManager } from './base.js';
|
4
|
+
export class StdioConnectionManager extends ConnectionManager {
|
5
|
+
serverParams;
|
6
|
+
errlog;
|
7
|
+
_transport = null;
|
8
|
+
/**
|
9
|
+
* Create a new stdio connection manager.
|
10
|
+
*
|
11
|
+
* @param serverParams Parameters for the stdio server process.
|
12
|
+
* @param errlog Stream to which the server's stderr should be piped.
|
13
|
+
* Defaults to `process.stderr`.
|
14
|
+
*/
|
15
|
+
constructor(serverParams, errlog = process.stderr) {
|
16
|
+
super();
|
17
|
+
this.serverParams = serverParams;
|
18
|
+
this.errlog = errlog;
|
19
|
+
}
|
20
|
+
/**
|
21
|
+
* Establish the stdio connection by spawning the server process and starting
|
22
|
+
* the SDK's transport. Returns the live `StdioClientTransport` instance.
|
23
|
+
*/
|
24
|
+
async establishConnection() {
|
25
|
+
// Instantiate and start the transport
|
26
|
+
this._transport = new StdioClientTransport(this.serverParams);
|
27
|
+
// await this._transport.start()
|
28
|
+
// If stderr was piped, forward it to `errlog` for visibility
|
29
|
+
if (this._transport.stderr && typeof this._transport.stderr.pipe === 'function') {
|
30
|
+
this._transport.stderr.pipe(this.errlog);
|
31
|
+
}
|
32
|
+
logger.debug(`${this.constructor.name} connected successfully`);
|
33
|
+
return this._transport;
|
34
|
+
}
|
35
|
+
/**
|
36
|
+
* Close the stdio connection, making sure the transport cleans up the child
|
37
|
+
* process and associated resources.
|
38
|
+
*/
|
39
|
+
async closeConnection(_connection) {
|
40
|
+
if (this._transport) {
|
41
|
+
try {
|
42
|
+
await this._transport.close();
|
43
|
+
}
|
44
|
+
catch (e) {
|
45
|
+
logger.warn(`Error closing stdio transport: ${e}`);
|
46
|
+
}
|
47
|
+
finally {
|
48
|
+
this._transport = null;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
52
|
+
}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
import WS from 'ws';
|
2
|
+
import { logger } from '../logging.js';
|
3
|
+
import { ConnectionManager } from './base.js';
|
4
|
+
export class WebSocketConnectionManager extends ConnectionManager {
|
5
|
+
url;
|
6
|
+
headers;
|
7
|
+
_ws = null;
|
8
|
+
/**
|
9
|
+
* @param url The WebSocket URL to connect to.
|
10
|
+
* @param headers Optional headers to include in the connection handshake.
|
11
|
+
*/
|
12
|
+
constructor(url, headers = {}) {
|
13
|
+
super();
|
14
|
+
this.url = url;
|
15
|
+
this.headers = headers;
|
16
|
+
}
|
17
|
+
/** Establish a WebSocket connection and wait until it is open. */
|
18
|
+
async establishConnection() {
|
19
|
+
logger.debug(`Connecting to WebSocket: ${this.url}`);
|
20
|
+
return new Promise((resolve, reject) => {
|
21
|
+
const ws = new WS(this.url, { headers: this.headers });
|
22
|
+
this._ws = ws;
|
23
|
+
const onOpen = () => {
|
24
|
+
// eslint-disable-next-line ts/no-use-before-define
|
25
|
+
cleanup();
|
26
|
+
logger.debug('WebSocket connected successfully');
|
27
|
+
resolve(ws);
|
28
|
+
};
|
29
|
+
const onError = (err) => {
|
30
|
+
// eslint-disable-next-line ts/no-use-before-define
|
31
|
+
cleanup();
|
32
|
+
logger.error(`Failed to connect to WebSocket: ${err}`);
|
33
|
+
reject(err);
|
34
|
+
};
|
35
|
+
const cleanup = () => {
|
36
|
+
ws.off('open', onOpen);
|
37
|
+
ws.off('error', onError);
|
38
|
+
};
|
39
|
+
// Register listeners (browser vs Node API differences handled)
|
40
|
+
ws.on('open', onOpen);
|
41
|
+
ws.on('error', onError);
|
42
|
+
});
|
43
|
+
}
|
44
|
+
/** Cleanly close the WebSocket connection. */
|
45
|
+
async closeConnection(connection) {
|
46
|
+
logger.debug('Closing WebSocket connection');
|
47
|
+
return new Promise((resolve) => {
|
48
|
+
const onClose = () => {
|
49
|
+
connection.off('close', onClose);
|
50
|
+
this._ws = null;
|
51
|
+
resolve();
|
52
|
+
};
|
53
|
+
if (connection.readyState === WS.CLOSED) {
|
54
|
+
onClose();
|
55
|
+
return;
|
56
|
+
}
|
57
|
+
connection.on('close', onClose);
|
58
|
+
try {
|
59
|
+
connection.close();
|
60
|
+
}
|
61
|
+
catch (e) {
|
62
|
+
logger.warn(`Error closing WebSocket connection: ${e}`);
|
63
|
+
onClose();
|
64
|
+
}
|
65
|
+
});
|
66
|
+
}
|
67
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
{
|
2
|
+
"name": "mcp-use",
|
3
|
+
"type": "module",
|
4
|
+
"version": "0.0.1",
|
5
|
+
"description": "A utility library for integrating Model Context Protocol (MCP) with LangChain, Zod, and related tools. Provides helpers for schema conversion, event streaming, and SDK usage.",
|
6
|
+
"author": "Zane",
|
7
|
+
"license": "MIT",
|
8
|
+
"homepage": "https://github.com/zandko/mcp-use#readme",
|
9
|
+
"repository": {
|
10
|
+
"type": "git",
|
11
|
+
"url": "git+https://github.com/zandko/mcp-use.git"
|
12
|
+
},
|
13
|
+
"bugs": {
|
14
|
+
"url": "https://github.com/zandko/mcp-use/issues"
|
15
|
+
},
|
16
|
+
"keywords": [
|
17
|
+
"MCP",
|
18
|
+
"Model Context Protocol",
|
19
|
+
"LangChain",
|
20
|
+
"Zod",
|
21
|
+
"schema",
|
22
|
+
"SDK",
|
23
|
+
"eventsource",
|
24
|
+
"AI",
|
25
|
+
"utility",
|
26
|
+
"typescript"
|
27
|
+
],
|
28
|
+
"exports": {
|
29
|
+
".": {
|
30
|
+
"types": "./dist/index.d.ts",
|
31
|
+
"import": "./dist/index.js"
|
32
|
+
}
|
33
|
+
},
|
34
|
+
"main": "./dist/index.js",
|
35
|
+
"module": "./dist/index.js",
|
36
|
+
"types": "./dist/index.d.ts",
|
37
|
+
"files": [
|
38
|
+
"dist"
|
39
|
+
],
|
40
|
+
"publishConfig": {
|
41
|
+
"registry": "https://registry.npmjs.org"
|
42
|
+
},
|
43
|
+
"scripts": {
|
44
|
+
"build": "tsc && shx chmod +x dist/*.js",
|
45
|
+
"watch": "tsc --watch",
|
46
|
+
"start": "node dist/index.js",
|
47
|
+
"prepublishOnly": "pnpm run build",
|
48
|
+
"lint": "eslint",
|
49
|
+
"lint:fix": "eslint --fix",
|
50
|
+
"release": "npm version patch && git push --follow-tags",
|
51
|
+
"release:minor": "npm version minor && git push --follow-tags",
|
52
|
+
"release:major": "npm version major && git push --follow-tags"
|
53
|
+
},
|
54
|
+
"dependencies": {
|
55
|
+
"@dmitryrechkin/json-schema-to-zod": "^1.0.1",
|
56
|
+
"@langchain/community": "0.3.41",
|
57
|
+
"@langchain/core": "0.3.45",
|
58
|
+
"@modelcontextprotocol/sdk": "1.10.1",
|
59
|
+
"eventsource": "^3.0.6",
|
60
|
+
"langchain": "^0.3.23",
|
61
|
+
"uuid": "^11.1.0",
|
62
|
+
"winston": "^3.17.0",
|
63
|
+
"ws": "^8.18.1",
|
64
|
+
"zod": "^3.24.3",
|
65
|
+
"zod-to-json-schema": "^3.24.5"
|
66
|
+
},
|
67
|
+
"devDependencies": {
|
68
|
+
"@antfu/eslint-config": "^4.12.0",
|
69
|
+
"@types/ws": "^8.18.1",
|
70
|
+
"eslint": "^9.24.0",
|
71
|
+
"eslint-plugin-format": "^1.0.1",
|
72
|
+
"shx": "^0.4.0",
|
73
|
+
"typescript": "^5.8.3"
|
74
|
+
}
|
75
|
+
}
|