-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-transport.js
More file actions
94 lines (79 loc) · 2.44 KB
/
Copy pathmcp-transport.js
File metadata and controls
94 lines (79 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import Logger from './utils/logger.js';
const logger = new Logger({
logLevel: process.env.LOG_LEVEL || 'info',
logToFile: true,
logToConsole: true,
functionName: 'mcp-transport',
});
class FuncDockMCPTransport {
constructor(options = {}) {
this.mcpServerUrl =
options.mcpServerUrl || process.env.MCP_SERVER_URL || 'http://localhost:3001';
this.client = null;
this.connected = false;
}
async connectHTTP() {
this.client = new Client({
name: 'funcdock-mcp-transport',
version: '2.0.0',
});
const transport = new SSEClientTransport(new URL(this.mcpServerUrl));
await this.client.connect(transport);
this.connected = true;
logger.info(`Connected to MCP server at ${this.mcpServerUrl}`);
return this;
}
async connectStdio(command, args = [], env = {}) {
this.client = new Client({
name: 'funcdock-mcp-transport',
version: '2.0.0',
});
const transport = new StdioClientTransport({
command,
args,
env,
});
await this.client.connect(transport);
this.connected = true;
logger.info(`Connected to MCP server via stdio: ${command} ${args.join(' ')}`);
return this;
}
async invokeFunction(functionName, options = {}) {
if (!this.connected || !this.client) {
throw new Error('MCP transport not connected');
}
const routePath = options.routePath || '/';
const method = options.method || 'POST';
const toolName = `${functionName}__${routePath.replace(/[/:]/g, '_').replace(/^_/, '')}__${method.toLowerCase()}`;
const result = await this.client.callTool({
name: toolName,
arguments: {
functionName,
routePath,
method,
body: options.body || {},
query: options.query || {},
params: options.params || {},
},
});
return result;
}
async listTools() {
if (!this.connected || !this.client) {
throw new Error('MCP transport not connected');
}
const { tools } = await this.client.listTools();
return tools;
}
async disconnect() {
if (this.client) {
this.client.close();
this.connected = false;
logger.info('Disconnected from MCP server');
}
}
}
export default FuncDockMCPTransport;