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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#!/usr/bin/env node
import * as path from "node:path";
import * as fs from "node:fs";
import * as os from "node:os";
import { v4 as uuid } from "uuid";
import { spawn } from "node:child_process";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ErrorCode,
ListToolsRequestSchema,
McpError,
} from "@modelcontextprotocol/sdk/types.js";
let [rootDir, agentName] = process.argv.slice(2);
if (!rootDir) {
console.error("Usage: mcp-llm-functions <llm-functions-dir> [<agent-name>]");
process.exit(1);
}
rootDir = path.resolve(rootDir);
let functionsJsonPath = path.join(rootDir, "functions.json");
if (agentName) {
functionsJsonPath = path.join(rootDir, "agents", agentName, "functions.json");
}
let functions = [];
try {
const data = await fs.promises.readFile(functionsJsonPath, "utf8");
functions = JSON.parse(data);
} catch {
console.error(`Failed to read functions at '${functionsJsonPath}'`);
process.exit(1);
}
const agentToolsOnly = process.env["AGENT_TOOLS_ONLY"] === "true" || process.env["AGENT_TOOLS_ONLY"] === "1";
functions = functions.filter(f => {
if (f.mcp) {
return false;
}
if (agentToolsOnly) {
return f.agent;
} else {
return true;
}
});
const env = Object.assign({}, process.env, {
PATH: `${path.join(rootDir, "bin")}:${process.env.PATH}`
});
const server = new Server(
{
name: `llm-functions/${agentName || "common-tools"}`,
version: "0.1.0",
},
{
capabilities: {
tools: {},
},
},
);
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: functions.map((f) => ({
name: f.name,
description: f.description,
inputSchema: f.parameters,
})),
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const functionObj = functions.find((f) => f.name === request.params.name);
if (!functionObj) {
throw new McpError(ErrorCode.InvalidRequest, `Unexpected tool '${request.params.name}'`);
}
let command = request.params.name;
let args = [JSON.stringify(request.params.arguments || {})];
if (agentName && functionObj.agent) {
args.unshift(command);
command = agentName;
}
const tmpFile = path.join(os.tmpdir(), `mcp-llm-functions-${process.pid}-eval-${uuid()}`);
const { exitCode, stderr } = await runCommand(command, args, { ...env, LLM_OUTPUT: tmpFile });
if (exitCode === 0) {
let output = '';
try {
output = await fs.promises.readFile(tmpFile, "utf8");
} catch { };
return {
content: [{ type: "text", value: output }],
}
} else {
return {
isError: true,
error: stderr,
};
}
});
function runCommand(command, args, env) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
stdio: ['ignore', 'ignore', 'pipe'],
env,
});
let stderr = '';
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.on('close', (exitCode) => {
resolve({ exitCode, stderr });
});
child.on('error', (err) => {
reject(err);
});
});
}
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
runServer().catch(console.error);
|