mcpboot 0.1.0
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/README.md +221 -0
- package/dist/index.js +997 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# mcpboot
|
|
2
|
+
|
|
3
|
+
Generate and serve an MCP server from a natural language prompt.
|
|
4
|
+
|
|
5
|
+
Point mcpboot at API documentation (or just describe what you want), and it generates a working [MCP](https://modelcontextprotocol.io/) server. No SDK knowledge required. No boilerplate. No code to maintain.
|
|
6
|
+
|
|
7
|
+
The LLM is used **only at startup** for code generation. At runtime, tool calls execute cached JavaScript with no LLM involvement and no per-call API costs.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install -g mcpboot
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Node.js 18+.
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Set your LLM API key
|
|
21
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
22
|
+
|
|
23
|
+
# Generate an MCP server for the Hacker News API
|
|
24
|
+
mcpboot --prompt "Create MCP tools for the Hacker News API: https://github.com/HackerNews/API"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
mcpboot fetches the API docs, generates tool handlers, and starts serving on `http://localhost:8000/mcp`.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
mcpboot [options]
|
|
33
|
+
|
|
34
|
+
Options:
|
|
35
|
+
--prompt <text> Generation prompt (inline)
|
|
36
|
+
--prompt-file <path> Generation prompt from file
|
|
37
|
+
|
|
38
|
+
--provider <name> LLM provider: anthropic | openai (default: anthropic)
|
|
39
|
+
--model <id> LLM model ID (default: provider-specific)
|
|
40
|
+
--api-key <key> LLM API key (env: ANTHROPIC_API_KEY | OPENAI_API_KEY)
|
|
41
|
+
|
|
42
|
+
--port <number> HTTP server port (default: 8000)
|
|
43
|
+
--cache-dir <path> Cache directory (default: .mcpboot-cache)
|
|
44
|
+
--no-cache Disable caching, regenerate on every startup
|
|
45
|
+
--verbose Verbose logging
|
|
46
|
+
--dry-run Show generation plan without starting server
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Examples
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Wrap the Hacker News API
|
|
53
|
+
mcpboot --prompt "Create MCP tools for the Hacker News API: https://github.com/HackerNews/API"
|
|
54
|
+
|
|
55
|
+
# Wrap an API from an OpenAPI spec
|
|
56
|
+
mcpboot --prompt "Create MCP tools from https://petstore.swagger.io/v2/swagger.json"
|
|
57
|
+
|
|
58
|
+
# Create specific tools from a known API
|
|
59
|
+
mcpboot --prompt "Using the GitHub REST API (https://docs.github.com/en/rest), \
|
|
60
|
+
create tools for listing repos, creating issues, and searching code"
|
|
61
|
+
|
|
62
|
+
# Create utility tools (no external API needed)
|
|
63
|
+
mcpboot --prompt "Create tools for JSON manipulation: pretty-print, validate, diff, and JSONPath extraction"
|
|
64
|
+
|
|
65
|
+
# Complex prompt from file
|
|
66
|
+
mcpboot --prompt-file ./my-api-prompt.txt --port 9000
|
|
67
|
+
|
|
68
|
+
# Preview what would be generated
|
|
69
|
+
mcpboot --prompt "Create MCP tools for https://github.com/HackerNews/API" --dry-run
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Walkthrough: Hacker News API
|
|
73
|
+
|
|
74
|
+
Here's a complete example of generating an MCP server for the [Hacker News API](https://github.com/HackerNews/API).
|
|
75
|
+
|
|
76
|
+
**1. Start mcpboot:**
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
mcpboot --model claude-haiku-4-5 --port 8100 --verbose \
|
|
80
|
+
--prompt "Create MCP tools for the Hacker News API. The API docs are at
|
|
81
|
+
https://github.com/HackerNews/API . Figure out what tools are appropriate
|
|
82
|
+
to expose — things like getting top stories, new stories, getting an item
|
|
83
|
+
by ID, getting a user profile, etc."
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
mcpboot fetches the API docs from GitHub, uses the LLM to plan and compile 10 tools, and starts serving:
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
[mcpboot] Found 1 URL(s) in prompt
|
|
90
|
+
[mcpboot] Fetching https://raw.githubusercontent.com/HackerNews/API/HEAD/README.md
|
|
91
|
+
[mcpboot] Fetched 1 page(s)
|
|
92
|
+
[mcpboot] Whitelist: github.com, firebase.google.com, hacker-news.firebaseio.com, ...
|
|
93
|
+
[mcpboot] Cache miss — generating tools via LLM
|
|
94
|
+
[mcpboot] Plan: 10 tool(s)
|
|
95
|
+
[mcpboot] Compiling handler for tool: get_top_stories
|
|
96
|
+
...
|
|
97
|
+
[mcpboot] Compiled 10 handler(s)
|
|
98
|
+
[mcpboot] Listening on http://localhost:8100/mcp
|
|
99
|
+
[mcpboot] Serving 10 tool(s)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**2. Test with the MCP Inspector:**
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npx @modelcontextprotocol/inspector --transport http --server-url http://localhost:8100/mcp
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Or test from the CLI with [mcporter](https://github.com/steipete/mcporter):
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# List all generated tools
|
|
112
|
+
npx mcporter list http://localhost:8100/mcp --schema --allow-http
|
|
113
|
+
|
|
114
|
+
# Get the top 5 stories (returns story IDs)
|
|
115
|
+
npx mcporter call 'http://localhost:8100/mcp.get_top_stories' limit=5 --allow-http
|
|
116
|
+
|
|
117
|
+
# Fetch details for a story
|
|
118
|
+
npx mcporter call 'http://localhost:8100/mcp.get_item_by_id' item_id=42345678 --allow-http
|
|
119
|
+
|
|
120
|
+
# Look up a user profile
|
|
121
|
+
npx mcporter call 'http://localhost:8100/mcp.get_user_profile' username=dang --allow-http
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Generated tools:** `get_top_stories`, `get_new_stories`, `get_best_stories`, `get_ask_stories`, `get_show_stories`, `get_job_stories`, `get_item_by_id`, `get_user_profile`, `get_max_item_id`, `get_recent_changes`
|
|
125
|
+
|
|
126
|
+
Subsequent runs with the same prompt skip the LLM entirely and start instantly from cache.
|
|
127
|
+
|
|
128
|
+
### Connecting from an MCP Host
|
|
129
|
+
|
|
130
|
+
Once mcpboot is running, connect any MCP-compatible host to `http://localhost:8000/mcp`. For example, in Claude Desktop's config:
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
{
|
|
134
|
+
"mcpServers": {
|
|
135
|
+
"my-api": {
|
|
136
|
+
"url": "http://localhost:8000/mcp"
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## How It Works
|
|
143
|
+
|
|
144
|
+
mcpboot follows a two-phase startup, then serves tools at runtime:
|
|
145
|
+
|
|
146
|
+
**Startup (LLM-assisted):**
|
|
147
|
+
|
|
148
|
+
1. **Fetch** — Extract URLs from the prompt, fetch their content (API docs, READMEs, OpenAPI specs), and build a domain whitelist for runtime network access.
|
|
149
|
+
2. **Plan** — Send the prompt and fetched docs to the LLM, which produces a structured generation plan: what tools to create, their schemas, which API endpoints they use.
|
|
150
|
+
3. **Compile** — Send each planned tool back to the LLM, which generates a JavaScript handler function that calls the API, parses responses, and formats results.
|
|
151
|
+
4. **Cache** — Store the plan and compiled handlers on disk, keyed by prompt hash + content hash. Subsequent startups with the same prompt and unchanged docs skip the LLM entirely.
|
|
152
|
+
|
|
153
|
+
**Runtime (no LLM):**
|
|
154
|
+
|
|
155
|
+
5. **Serve** — Expose the generated tools as an MCP server over StreamableHTTP. Tool calls execute the cached JavaScript handlers in a sandboxed `vm` with fetch access restricted to whitelisted domains.
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
Prompt + API Docs
|
|
159
|
+
│
|
|
160
|
+
▼
|
|
161
|
+
URL Fetcher ──► Planner (LLM) ──► Compiler (LLM) ──► Cache
|
|
162
|
+
│
|
|
163
|
+
▼
|
|
164
|
+
MCP Host ◄──► Exposed Server ◄──► Executor ◄──► Sandbox (vm + fetch)
|
|
165
|
+
│
|
|
166
|
+
▼
|
|
167
|
+
External APIs
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Security Model
|
|
171
|
+
|
|
172
|
+
Generated handlers run in a Node.js `vm` sandbox with:
|
|
173
|
+
|
|
174
|
+
- **Allowed:** Standard JS globals, `fetch` (whitelisted domains only), URL, URLSearchParams
|
|
175
|
+
- **Blocked:** `require`, `import`, `process`, `fs`, `net`, `child_process`
|
|
176
|
+
- **Timeout:** 30 seconds per tool call
|
|
177
|
+
|
|
178
|
+
The domain whitelist is constructed automatically from URLs in the prompt and URLs discovered in the fetched documentation.
|
|
179
|
+
|
|
180
|
+
## Comparison with mcpblox
|
|
181
|
+
|
|
182
|
+
mcpboot and [mcpblox](https://github.com/vivekhaldar/mcpblox) are complementary:
|
|
183
|
+
|
|
184
|
+
| | mcpblox | mcpboot |
|
|
185
|
+
|--|---------|---------|
|
|
186
|
+
| **Input** | Existing MCP server + transform prompt | Natural language prompt + optional API docs |
|
|
187
|
+
| **Output** | Transformed MCP server | New MCP server from scratch |
|
|
188
|
+
| **LLM generates** | Transform functions (input/output mappers) | Tool handler functions (full API integrations) |
|
|
189
|
+
| **Use case** | Customize an existing server | Create a server that doesn't exist yet |
|
|
190
|
+
|
|
191
|
+
They can be chained: use mcpboot to bootstrap a server, then mcpblox to transform it.
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
# Bootstrap an MCP server for the HN API
|
|
195
|
+
mcpboot --prompt "Create MCP tools for https://github.com/HackerNews/API" --port 8001
|
|
196
|
+
|
|
197
|
+
# Transform it with mcpblox to add higher-level tools
|
|
198
|
+
mcpblox --upstream-url http://localhost:8001/mcp \
|
|
199
|
+
--prompt "Create a 'daily_digest' tool that gets top 10 stories with their top comments" \
|
|
200
|
+
--port 8002
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Development
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
# Install dependencies
|
|
207
|
+
npm install
|
|
208
|
+
|
|
209
|
+
# Run tests
|
|
210
|
+
npm test
|
|
211
|
+
|
|
212
|
+
# Build
|
|
213
|
+
npm run build
|
|
214
|
+
|
|
215
|
+
# Run from source
|
|
216
|
+
npx tsx src/index.ts --prompt "..."
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
|
|
221
|
+
Apache-2.0
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,997 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/config.ts
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
6
|
+
function buildConfig(argv) {
|
|
7
|
+
const program = new Command().name("mcpboot").description(
|
|
8
|
+
"Generate and serve an MCP server from a natural language prompt"
|
|
9
|
+
).option("--prompt <text>", "Generation prompt (inline)").option("--prompt-file <path>", "Generation prompt from file").option(
|
|
10
|
+
"--provider <name>",
|
|
11
|
+
"LLM provider: anthropic | openai",
|
|
12
|
+
"anthropic"
|
|
13
|
+
).option("--model <id>", "LLM model ID").option("--api-key <key>", "LLM API key").option("--port <number>", "HTTP server port", "8000").option("--cache-dir <path>", "Cache directory", ".mcpboot-cache").option("--no-cache", "Disable caching").option("--verbose", "Verbose logging", false).option(
|
|
14
|
+
"--dry-run",
|
|
15
|
+
"Show generation plan without starting server",
|
|
16
|
+
false
|
|
17
|
+
).exitOverride().configureOutput({
|
|
18
|
+
writeErr: () => {
|
|
19
|
+
},
|
|
20
|
+
writeOut: (str) => process.stdout.write(str)
|
|
21
|
+
});
|
|
22
|
+
try {
|
|
23
|
+
program.parse(argv);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
if (err && typeof err === "object" && "code" in err && err.code === "commander.helpDisplayed") {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
throw err;
|
|
29
|
+
}
|
|
30
|
+
const opts = program.opts();
|
|
31
|
+
if (opts.provider !== "anthropic" && opts.provider !== "openai") {
|
|
32
|
+
throw new Error("Error: --provider must be 'anthropic' or 'openai'");
|
|
33
|
+
}
|
|
34
|
+
let prompt;
|
|
35
|
+
if (opts.promptFile) {
|
|
36
|
+
if (opts.prompt) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"Error: Provide exactly one of --prompt or --prompt-file, not both"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
if (!existsSync(opts.promptFile)) {
|
|
42
|
+
throw new Error(`Error: File not found: ${opts.promptFile}`);
|
|
43
|
+
}
|
|
44
|
+
prompt = readFileSync(opts.promptFile, "utf-8");
|
|
45
|
+
} else if (opts.prompt) {
|
|
46
|
+
prompt = opts.prompt;
|
|
47
|
+
} else {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"Error: Provide --prompt or --prompt-file to specify the generation prompt"
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (!prompt.trim()) {
|
|
53
|
+
throw new Error("Error: Prompt is empty. Provide a non-empty generation prompt");
|
|
54
|
+
}
|
|
55
|
+
let apiKey = opts.apiKey;
|
|
56
|
+
if (!apiKey) {
|
|
57
|
+
if (opts.provider === "anthropic") {
|
|
58
|
+
apiKey = process.env.ANTHROPIC_API_KEY;
|
|
59
|
+
} else {
|
|
60
|
+
apiKey = process.env.OPENAI_API_KEY;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!apiKey) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"Error: No API key found. Provide --api-key or set ANTHROPIC_API_KEY / OPENAI_API_KEY"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const port = parseInt(opts.port, 10);
|
|
69
|
+
if (isNaN(port) || port < 0 || port > 65535) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"Error: --port must be a valid integer between 0 and 65535"
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
prompt,
|
|
76
|
+
llm: {
|
|
77
|
+
provider: opts.provider,
|
|
78
|
+
model: opts.model,
|
|
79
|
+
apiKey
|
|
80
|
+
},
|
|
81
|
+
server: {
|
|
82
|
+
port
|
|
83
|
+
},
|
|
84
|
+
cache: {
|
|
85
|
+
enabled: opts.cache !== false,
|
|
86
|
+
dir: opts.cacheDir ?? ".mcpboot-cache"
|
|
87
|
+
},
|
|
88
|
+
dryRun: opts.dryRun ?? false,
|
|
89
|
+
verbose: opts.verbose ?? false
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/log.ts
|
|
94
|
+
var verboseEnabled = false;
|
|
95
|
+
function setVerbose(enabled) {
|
|
96
|
+
verboseEnabled = enabled;
|
|
97
|
+
}
|
|
98
|
+
function log(msg) {
|
|
99
|
+
console.error(`[mcpboot] ${msg}`);
|
|
100
|
+
}
|
|
101
|
+
function warn(msg) {
|
|
102
|
+
console.error(`[mcpboot] WARN: ${msg}`);
|
|
103
|
+
}
|
|
104
|
+
function verbose(msg) {
|
|
105
|
+
if (verboseEnabled) console.error(`[mcpboot] ${msg}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/fetcher.ts
|
|
109
|
+
var URL_REGEX = /(https?:\/\/[^\s"'<>)\]]+)/g;
|
|
110
|
+
var GITHUB_REPO_REGEX = /^https?:\/\/github\.com\/([^/]+)\/([^/]+)\/?$/;
|
|
111
|
+
var MAX_CONTENT_LENGTH = 1e5;
|
|
112
|
+
var FETCH_TIMEOUT_MS = 15e3;
|
|
113
|
+
function extractUrls(prompt) {
|
|
114
|
+
const matches = prompt.match(URL_REGEX);
|
|
115
|
+
if (!matches) return [];
|
|
116
|
+
const cleaned = matches.map((url) => url.replace(/[.,;:!?)]+$/, ""));
|
|
117
|
+
return [...new Set(cleaned)];
|
|
118
|
+
}
|
|
119
|
+
function rewriteGitHubUrl(url) {
|
|
120
|
+
const match = url.match(GITHUB_REPO_REGEX);
|
|
121
|
+
if (!match) return null;
|
|
122
|
+
const [, owner, repo] = match;
|
|
123
|
+
return `https://raw.githubusercontent.com/${owner}/${repo}/HEAD/README.md`;
|
|
124
|
+
}
|
|
125
|
+
function stripHtml(html) {
|
|
126
|
+
let text = html;
|
|
127
|
+
text = text.replace(
|
|
128
|
+
/<(script|style|nav|header|footer)\b[^>]*>[\s\S]*?<\/\1>/gi,
|
|
129
|
+
""
|
|
130
|
+
);
|
|
131
|
+
text = text.replace(/<[^>]+>/g, " ");
|
|
132
|
+
text = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, " ");
|
|
133
|
+
text = text.replace(/\s+/g, " ").trim();
|
|
134
|
+
return text;
|
|
135
|
+
}
|
|
136
|
+
function discoverUrls(content) {
|
|
137
|
+
return extractUrls(content);
|
|
138
|
+
}
|
|
139
|
+
function truncateContent(content, limit = MAX_CONTENT_LENGTH) {
|
|
140
|
+
if (content.length <= limit) return content;
|
|
141
|
+
return content.slice(0, limit);
|
|
142
|
+
}
|
|
143
|
+
async function fetchUrl(url) {
|
|
144
|
+
const fetchTarget = rewriteGitHubUrl(url) ?? url;
|
|
145
|
+
verbose(`Fetching ${fetchTarget}`);
|
|
146
|
+
const response = await fetch(fetchTarget, {
|
|
147
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
148
|
+
headers: {
|
|
149
|
+
"User-Agent": "mcpboot/1.0"
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
if (!response.ok) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`Fetch failed for ${url}: ${response.status} ${response.statusText}`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
const rawContentType = response.headers.get("content-type") ?? "text/plain";
|
|
158
|
+
const contentType = rawContentType.split(";")[0].trim();
|
|
159
|
+
let text = await response.text();
|
|
160
|
+
if (contentType === "text/html") {
|
|
161
|
+
text = stripHtml(text);
|
|
162
|
+
}
|
|
163
|
+
text = truncateContent(text);
|
|
164
|
+
const discovered = discoverUrls(text);
|
|
165
|
+
return {
|
|
166
|
+
url,
|
|
167
|
+
content: text,
|
|
168
|
+
contentType,
|
|
169
|
+
discoveredUrls: discovered
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
async function fetchUrls(urls) {
|
|
173
|
+
if (urls.length === 0) return [];
|
|
174
|
+
const results = await Promise.allSettled(urls.map((url) => fetchUrl(url)));
|
|
175
|
+
const contents = [];
|
|
176
|
+
for (const result of results) {
|
|
177
|
+
if (result.status === "fulfilled") {
|
|
178
|
+
contents.push(result.value);
|
|
179
|
+
} else {
|
|
180
|
+
warn(`Failed to fetch URL: ${result.reason}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return contents;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// src/whitelist.ts
|
|
187
|
+
function extractDomain(url) {
|
|
188
|
+
try {
|
|
189
|
+
return new URL(url).hostname;
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function domainMatches(hostname, whitelistedDomain) {
|
|
195
|
+
if (hostname === whitelistedDomain) return true;
|
|
196
|
+
return hostname.endsWith("." + whitelistedDomain);
|
|
197
|
+
}
|
|
198
|
+
function buildWhitelist(promptUrls, contents) {
|
|
199
|
+
const domains = /* @__PURE__ */ new Set();
|
|
200
|
+
for (const url of promptUrls) {
|
|
201
|
+
const domain = extractDomain(url);
|
|
202
|
+
if (domain) domains.add(domain);
|
|
203
|
+
}
|
|
204
|
+
for (const content of contents) {
|
|
205
|
+
for (const url of content.discoveredUrls) {
|
|
206
|
+
const domain = extractDomain(url);
|
|
207
|
+
if (domain) domains.add(domain);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
domains,
|
|
212
|
+
allows(url) {
|
|
213
|
+
const hostname = extractDomain(url);
|
|
214
|
+
if (!hostname) return false;
|
|
215
|
+
for (const d of domains) {
|
|
216
|
+
if (domainMatches(hostname, d)) return true;
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function createWhitelistedFetch(whitelist, realFetch = globalThis.fetch) {
|
|
223
|
+
return (url) => {
|
|
224
|
+
const hostname = extractDomain(url);
|
|
225
|
+
if (!hostname) {
|
|
226
|
+
return Promise.reject(
|
|
227
|
+
new Error(`Fetch blocked: invalid URL "${url}"`)
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
if (!whitelist.allows(url)) {
|
|
231
|
+
return Promise.reject(
|
|
232
|
+
new Error(
|
|
233
|
+
`Fetch blocked: domain "${hostname}" not in whitelist. Add it to your prompt to allow access.`
|
|
234
|
+
)
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return realFetch(url);
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/cache.ts
|
|
242
|
+
import { createHash } from "node:crypto";
|
|
243
|
+
import {
|
|
244
|
+
existsSync as existsSync2,
|
|
245
|
+
mkdirSync,
|
|
246
|
+
readFileSync as readFileSync2,
|
|
247
|
+
writeFileSync,
|
|
248
|
+
unlinkSync
|
|
249
|
+
} from "node:fs";
|
|
250
|
+
import { join } from "node:path";
|
|
251
|
+
function hash(input) {
|
|
252
|
+
return createHash("sha256").update(input).digest("hex").slice(0, 16);
|
|
253
|
+
}
|
|
254
|
+
function cacheFilename(promptHash, contentHash) {
|
|
255
|
+
return `${promptHash}-${contentHash}.json`;
|
|
256
|
+
}
|
|
257
|
+
function serializeCompiled(compiled) {
|
|
258
|
+
const compiledTools = [];
|
|
259
|
+
for (const tool of compiled.tools.values()) {
|
|
260
|
+
compiledTools.push({
|
|
261
|
+
name: tool.name,
|
|
262
|
+
description: tool.description,
|
|
263
|
+
input_schema: tool.input_schema,
|
|
264
|
+
handler_code: tool.handler_code,
|
|
265
|
+
needs_network: tool.needs_network
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
return { compiledTools };
|
|
269
|
+
}
|
|
270
|
+
function deserializeCompiled(entry) {
|
|
271
|
+
const tools = /* @__PURE__ */ new Map();
|
|
272
|
+
for (const item of entry.compiledTools) {
|
|
273
|
+
tools.set(item.name, {
|
|
274
|
+
name: item.name,
|
|
275
|
+
description: item.description,
|
|
276
|
+
input_schema: item.input_schema,
|
|
277
|
+
handler_code: item.handler_code,
|
|
278
|
+
needs_network: item.needs_network
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
tools,
|
|
283
|
+
whitelist_domains: entry.whitelist_domains
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function createCache(config) {
|
|
287
|
+
return {
|
|
288
|
+
get(promptHash, contentHash) {
|
|
289
|
+
if (!config.enabled) return null;
|
|
290
|
+
const filepath = join(config.dir, cacheFilename(promptHash, contentHash));
|
|
291
|
+
if (!existsSync2(filepath)) return null;
|
|
292
|
+
verbose(`Cache lookup: ${filepath}`);
|
|
293
|
+
try {
|
|
294
|
+
const raw = readFileSync2(filepath, "utf-8");
|
|
295
|
+
const parsed = JSON.parse(raw);
|
|
296
|
+
if (!parsed.promptHash || !parsed.contentHash || !parsed.plan || !Array.isArray(parsed.compiledTools)) {
|
|
297
|
+
warn(`Corrupt cache file ${filepath}, removing`);
|
|
298
|
+
unlinkSync(filepath);
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
verbose(`Cache hit: ${filepath} (created ${parsed.createdAt})`);
|
|
302
|
+
return parsed;
|
|
303
|
+
} catch {
|
|
304
|
+
warn(`Failed to read cache file ${filepath}, removing`);
|
|
305
|
+
try {
|
|
306
|
+
unlinkSync(filepath);
|
|
307
|
+
} catch {
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
set(entry) {
|
|
313
|
+
if (!config.enabled) return;
|
|
314
|
+
mkdirSync(config.dir, { recursive: true });
|
|
315
|
+
const filepath = join(
|
|
316
|
+
config.dir,
|
|
317
|
+
cacheFilename(entry.promptHash, entry.contentHash)
|
|
318
|
+
);
|
|
319
|
+
writeFileSync(filepath, JSON.stringify(entry, null, 2));
|
|
320
|
+
verbose(`Cache written: ${filepath}`);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/llm.ts
|
|
326
|
+
import { generateText } from "ai";
|
|
327
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
328
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
329
|
+
var DEFAULT_MODELS = {
|
|
330
|
+
anthropic: "claude-sonnet-4-20250514",
|
|
331
|
+
openai: "gpt-4o"
|
|
332
|
+
};
|
|
333
|
+
function createLLMClient(config) {
|
|
334
|
+
const modelId = config.model ?? DEFAULT_MODELS[config.provider];
|
|
335
|
+
let model;
|
|
336
|
+
if (config.provider === "anthropic") {
|
|
337
|
+
const anthropic = createAnthropic({ apiKey: config.apiKey });
|
|
338
|
+
model = anthropic(modelId);
|
|
339
|
+
} else {
|
|
340
|
+
const openai = createOpenAI({ apiKey: config.apiKey });
|
|
341
|
+
model = openai(modelId);
|
|
342
|
+
}
|
|
343
|
+
return {
|
|
344
|
+
async generate(system, user) {
|
|
345
|
+
try {
|
|
346
|
+
const result = await generateText({
|
|
347
|
+
model,
|
|
348
|
+
system,
|
|
349
|
+
prompt: user,
|
|
350
|
+
maxTokens: 8192,
|
|
351
|
+
temperature: 0.2
|
|
352
|
+
});
|
|
353
|
+
return result.text;
|
|
354
|
+
} catch (error) {
|
|
355
|
+
const err = error;
|
|
356
|
+
if (err.statusCode === 404) {
|
|
357
|
+
throw new Error(
|
|
358
|
+
`Model "${modelId}" not found. Check the model ID \u2014 e.g. "claude-sonnet-4-20250514" or "claude-haiku-4-5" (note: dated variants like "claude-haiku-4-5-20241022" don't exist; use "claude-3-5-haiku-20241022" for the dated form)`
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/engine/planner.ts
|
|
368
|
+
var TOOL_NAME_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
369
|
+
var SYSTEM_PROMPT = `You are an MCP tool planner. You receive a natural language description of desired tools, optionally with API documentation content, and you produce a STRUCTURED PLAN (as JSON) describing the tools to generate.
|
|
370
|
+
|
|
371
|
+
OUTPUT FORMAT:
|
|
372
|
+
Return ONLY valid JSON matching this exact schema (no markdown, no explanation):
|
|
373
|
+
|
|
374
|
+
{
|
|
375
|
+
"tools": [
|
|
376
|
+
{
|
|
377
|
+
"name": "tool_name",
|
|
378
|
+
"description": "What the tool does",
|
|
379
|
+
"input_schema": {
|
|
380
|
+
"type": "object",
|
|
381
|
+
"properties": {
|
|
382
|
+
"param_name": { "type": "string", "description": "Parameter description" }
|
|
383
|
+
},
|
|
384
|
+
"required": ["param_name"]
|
|
385
|
+
},
|
|
386
|
+
"endpoints_used": ["GET https://api.example.com/items"],
|
|
387
|
+
"implementation_notes": "Detailed description of how to implement this handler: what URL to call, how to parse the response, what to return",
|
|
388
|
+
"needs_network": true
|
|
389
|
+
}
|
|
390
|
+
]
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
RULES:
|
|
394
|
+
1. Tool names must be lowercase with underscores only (a-z, 0-9, _). Must start with a letter.
|
|
395
|
+
2. Each tool must have a unique name.
|
|
396
|
+
3. input_schema must be valid JSON Schema with "type": "object".
|
|
397
|
+
4. endpoints_used lists the HTTP endpoints the tool will call. Use format "METHOD url".
|
|
398
|
+
5. implementation_notes must be detailed enough for a code generator to write the handler.
|
|
399
|
+
6. Set needs_network to true if the tool makes HTTP requests, false for pure computation.
|
|
400
|
+
7. If API documentation is provided, base the tools on the actual API endpoints documented.
|
|
401
|
+
8. Create focused, single-purpose tools. Prefer multiple simple tools over one complex tool.
|
|
402
|
+
9. The endpoints_used URLs must use domains from the provided whitelist.
|
|
403
|
+
10. For pure computation tools (no API calls), set endpoints_used to an empty array.`;
|
|
404
|
+
function extractJSON(text) {
|
|
405
|
+
const fenceMatch = text.match(/```(?:json)?\s*\n?([\s\S]*?)\n?```/);
|
|
406
|
+
if (fenceMatch) return fenceMatch[1].trim();
|
|
407
|
+
const jsonMatch = text.match(/\{[\s\S]*\}/);
|
|
408
|
+
if (jsonMatch) return jsonMatch[0];
|
|
409
|
+
return text.trim();
|
|
410
|
+
}
|
|
411
|
+
function validatePlan(plan) {
|
|
412
|
+
if (!plan.tools || !Array.isArray(plan.tools)) {
|
|
413
|
+
throw new Error("Invalid plan: missing or non-array 'tools' field");
|
|
414
|
+
}
|
|
415
|
+
if (plan.tools.length === 0) {
|
|
416
|
+
throw new Error("Invalid plan: 'tools' array is empty");
|
|
417
|
+
}
|
|
418
|
+
const names = /* @__PURE__ */ new Set();
|
|
419
|
+
for (const tool of plan.tools) {
|
|
420
|
+
if (!tool.name) {
|
|
421
|
+
throw new Error("Invalid plan: tool missing 'name'");
|
|
422
|
+
}
|
|
423
|
+
if (!TOOL_NAME_PATTERN.test(tool.name)) {
|
|
424
|
+
throw new Error(
|
|
425
|
+
`Invalid tool name "${tool.name}": must be lowercase letters, digits, and underscores, starting with a letter`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
if (names.has(tool.name)) {
|
|
429
|
+
throw new Error(`Duplicate tool name: "${tool.name}"`);
|
|
430
|
+
}
|
|
431
|
+
names.add(tool.name);
|
|
432
|
+
if (!tool.description) {
|
|
433
|
+
throw new Error(`Tool "${tool.name}": description is required`);
|
|
434
|
+
}
|
|
435
|
+
if (!tool.input_schema || typeof tool.input_schema !== "object") {
|
|
436
|
+
throw new Error(`Tool "${tool.name}": input_schema is required`);
|
|
437
|
+
}
|
|
438
|
+
if (!tool.implementation_notes) {
|
|
439
|
+
throw new Error(`Tool "${tool.name}": implementation_notes is required`);
|
|
440
|
+
}
|
|
441
|
+
if (typeof tool.needs_network !== "boolean") {
|
|
442
|
+
throw new Error(`Tool "${tool.name}": needs_network must be a boolean`);
|
|
443
|
+
}
|
|
444
|
+
if (!Array.isArray(tool.endpoints_used)) {
|
|
445
|
+
throw new Error(`Tool "${tool.name}": endpoints_used must be an array`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function validatePlanWhitelist(plan, whitelist) {
|
|
450
|
+
for (const tool of plan.tools) {
|
|
451
|
+
if (!tool.needs_network) continue;
|
|
452
|
+
for (const endpoint of tool.endpoints_used) {
|
|
453
|
+
const urlMatch = endpoint.match(/https?:\/\/[^\s]+/);
|
|
454
|
+
if (!urlMatch) continue;
|
|
455
|
+
if (!whitelist.allows(urlMatch[0])) {
|
|
456
|
+
throw new Error(
|
|
457
|
+
`Tool "${tool.name}" uses endpoint "${endpoint}" whose domain is not in the whitelist`
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
function buildUserPrompt(prompt, contents, whitelist) {
|
|
464
|
+
let userPrompt = `GENERATION PROMPT:
|
|
465
|
+
${prompt}
|
|
466
|
+
`;
|
|
467
|
+
if (contents.length > 0) {
|
|
468
|
+
userPrompt += "\nFETCHED API DOCUMENTATION:\n";
|
|
469
|
+
for (const content of contents) {
|
|
470
|
+
userPrompt += `
|
|
471
|
+
--- Source: ${content.url} ---
|
|
472
|
+
${content.content}
|
|
473
|
+
`;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
const domains = [...whitelist.domains];
|
|
477
|
+
if (domains.length > 0) {
|
|
478
|
+
userPrompt += `
|
|
479
|
+
ALLOWED DOMAINS (whitelist):
|
|
480
|
+
${domains.join("\n")}
|
|
481
|
+
`;
|
|
482
|
+
userPrompt += "\nGenerated tools may only make HTTP requests to these domains.\n";
|
|
483
|
+
} else {
|
|
484
|
+
userPrompt += "\nNo domains are whitelisted. Generate only pure computation tools (needs_network: false).\n";
|
|
485
|
+
}
|
|
486
|
+
return userPrompt;
|
|
487
|
+
}
|
|
488
|
+
async function generatePlan(llm, prompt, contents, whitelist) {
|
|
489
|
+
const userPrompt = buildUserPrompt(prompt, contents, whitelist);
|
|
490
|
+
let lastError = null;
|
|
491
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
492
|
+
let response;
|
|
493
|
+
try {
|
|
494
|
+
response = await llm.generate(SYSTEM_PROMPT, userPrompt);
|
|
495
|
+
} catch (error) {
|
|
496
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
497
|
+
throw new Error(`LLM error during planning: ${message}`);
|
|
498
|
+
}
|
|
499
|
+
const jsonText = extractJSON(response);
|
|
500
|
+
let parsed;
|
|
501
|
+
try {
|
|
502
|
+
parsed = JSON.parse(jsonText);
|
|
503
|
+
} catch {
|
|
504
|
+
lastError = new Error(
|
|
505
|
+
`Failed to parse plan JSON from LLM response: ${jsonText.slice(0, 200)}`
|
|
506
|
+
);
|
|
507
|
+
if (attempt === 0) {
|
|
508
|
+
warn("Invalid JSON from LLM, retrying...");
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
throw lastError;
|
|
512
|
+
}
|
|
513
|
+
try {
|
|
514
|
+
validatePlan(parsed);
|
|
515
|
+
} catch (error) {
|
|
516
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
517
|
+
if (attempt === 0) {
|
|
518
|
+
warn(`Invalid plan from LLM (${lastError.message}), retrying...`);
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
throw lastError;
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
validatePlanWhitelist(parsed, whitelist);
|
|
525
|
+
} catch (error) {
|
|
526
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
527
|
+
if (attempt === 0) {
|
|
528
|
+
warn(
|
|
529
|
+
`Plan whitelist violation (${lastError.message}), retrying...`
|
|
530
|
+
);
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
throw lastError;
|
|
534
|
+
}
|
|
535
|
+
verbose(`Generated plan:
|
|
536
|
+
${JSON.stringify(parsed, null, 2)}`);
|
|
537
|
+
return parsed;
|
|
538
|
+
}
|
|
539
|
+
throw lastError ?? new Error("Plan generation failed");
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/engine/compiler.ts
|
|
543
|
+
var SYSTEM_PROMPT_NETWORK = `You are a JavaScript code generator for MCP tool handlers. You write async function BODIES (not full function declarations) that:
|
|
544
|
+
|
|
545
|
+
- Receive \`args\` (the tool call arguments object) and \`fetch\` (a whitelisted fetch function) as parameters
|
|
546
|
+
- Make HTTP calls using \`fetch\` to the specified API endpoints
|
|
547
|
+
- Parse responses
|
|
548
|
+
- Return \`{ content: [{ type: "text", text: "..." }] }\`
|
|
549
|
+
- Handle errors gracefully with try/catch and meaningful error messages
|
|
550
|
+
|
|
551
|
+
Available globals: JSON, Math, String, Number, Boolean, Array, Object, Map, Set, Date, RegExp, Promise, URL, URLSearchParams, TextEncoder, TextDecoder, Headers, Response, fetch, console.log, parseInt, parseFloat, isNaN, isFinite
|
|
552
|
+
|
|
553
|
+
NOT available: require, import, process, fs, net, http, Buffer, setTimeout, setInterval
|
|
554
|
+
|
|
555
|
+
OUTPUT FORMAT:
|
|
556
|
+
Return ONLY the function body code. No function declaration, no exports, no imports. The code will be wrapped in \`(async function(args, fetch) { YOUR_CODE_HERE })(inputArgs, fetchFn)\`.
|
|
557
|
+
|
|
558
|
+
Example output:
|
|
559
|
+
\`\`\`javascript
|
|
560
|
+
const url = "https://api.example.com/items?limit=" + (args.limit || 10);
|
|
561
|
+
const res = await fetch(url);
|
|
562
|
+
if (!res.ok) {
|
|
563
|
+
return { content: [{ type: "text", text: "Error: " + res.status + " " + res.statusText }], isError: true };
|
|
564
|
+
}
|
|
565
|
+
const data = await res.json();
|
|
566
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
567
|
+
\`\`\``;
|
|
568
|
+
var SYSTEM_PROMPT_PURE = `You are a JavaScript code generator for MCP tool handlers. You write async function BODIES (not full function declarations) that:
|
|
569
|
+
|
|
570
|
+
- Receive \`args\` (the tool call arguments object) as a parameter
|
|
571
|
+
- Perform computation using the provided arguments
|
|
572
|
+
- Return \`{ content: [{ type: "text", text: "..." }] }\`
|
|
573
|
+
- Handle errors gracefully with try/catch and meaningful error messages
|
|
574
|
+
|
|
575
|
+
This tool does NOT have network access. Do NOT use fetch.
|
|
576
|
+
|
|
577
|
+
Available globals: JSON, Math, String, Number, Boolean, Array, Object, Map, Set, Date, RegExp, Promise, URL, URLSearchParams, TextEncoder, TextDecoder, console.log, parseInt, parseFloat, isNaN, isFinite
|
|
578
|
+
|
|
579
|
+
NOT available: require, import, process, fs, net, http, Buffer, setTimeout, setInterval, fetch
|
|
580
|
+
|
|
581
|
+
OUTPUT FORMAT:
|
|
582
|
+
Return ONLY the function body code. No function declaration, no exports, no imports. The code will be wrapped in \`(async function(args) { YOUR_CODE_HERE })(inputArgs)\`.
|
|
583
|
+
|
|
584
|
+
Example output:
|
|
585
|
+
\`\`\`javascript
|
|
586
|
+
const result = args.a + args.b;
|
|
587
|
+
return { content: [{ type: "text", text: String(result) }] };
|
|
588
|
+
\`\`\``;
|
|
589
|
+
function extractCode(text) {
|
|
590
|
+
const fenceMatch = text.match(
|
|
591
|
+
/```(?:javascript|js|typescript|ts)?\s*\n?([\s\S]*?)\n?```/
|
|
592
|
+
);
|
|
593
|
+
if (fenceMatch) return fenceMatch[1].trim();
|
|
594
|
+
return text.trim();
|
|
595
|
+
}
|
|
596
|
+
function validateCode(code) {
|
|
597
|
+
if (/\bimport\s+/.test(code)) {
|
|
598
|
+
throw new Error("Generated code must not use import statements");
|
|
599
|
+
}
|
|
600
|
+
if (/\brequire\s*\(/.test(code)) {
|
|
601
|
+
throw new Error("Generated code must not use require()");
|
|
602
|
+
}
|
|
603
|
+
try {
|
|
604
|
+
new Function(`return (async function(args, fetch) { ${code} });`);
|
|
605
|
+
} catch (error) {
|
|
606
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
607
|
+
throw new Error(`Invalid JavaScript syntax: ${message}`);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function buildHandlerPrompt(prompt, tool, contents) {
|
|
611
|
+
let userPrompt = `ORIGINAL PROMPT:
|
|
612
|
+
${prompt}
|
|
613
|
+
`;
|
|
614
|
+
userPrompt += `
|
|
615
|
+
TOOL TO IMPLEMENT:
|
|
616
|
+
`;
|
|
617
|
+
userPrompt += `Name: ${tool.name}
|
|
618
|
+
`;
|
|
619
|
+
userPrompt += `Description: ${tool.description}
|
|
620
|
+
`;
|
|
621
|
+
userPrompt += `Input Schema: ${JSON.stringify(tool.input_schema, null, 2)}
|
|
622
|
+
`;
|
|
623
|
+
userPrompt += `Implementation Notes: ${tool.implementation_notes}
|
|
624
|
+
`;
|
|
625
|
+
if (tool.needs_network) {
|
|
626
|
+
userPrompt += `
|
|
627
|
+
Endpoints Used:
|
|
628
|
+
`;
|
|
629
|
+
for (const endpoint of tool.endpoints_used) {
|
|
630
|
+
userPrompt += ` - ${endpoint}
|
|
631
|
+
`;
|
|
632
|
+
}
|
|
633
|
+
} else {
|
|
634
|
+
userPrompt += `
|
|
635
|
+
This tool does NOT need network access. Do not use fetch.
|
|
636
|
+
`;
|
|
637
|
+
}
|
|
638
|
+
if (contents.length > 0) {
|
|
639
|
+
userPrompt += `
|
|
640
|
+
API DOCUMENTATION:
|
|
641
|
+
`;
|
|
642
|
+
for (const content of contents) {
|
|
643
|
+
userPrompt += `
|
|
644
|
+
--- Source: ${content.url} ---
|
|
645
|
+
${content.content}
|
|
646
|
+
`;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return userPrompt;
|
|
650
|
+
}
|
|
651
|
+
async function compilePlan(llm, plan, contents) {
|
|
652
|
+
const tools = /* @__PURE__ */ new Map();
|
|
653
|
+
for (const plannedTool of plan.tools) {
|
|
654
|
+
verbose(`Compiling handler for tool: ${plannedTool.name}`);
|
|
655
|
+
const systemPrompt = plannedTool.needs_network ? SYSTEM_PROMPT_NETWORK : SYSTEM_PROMPT_PURE;
|
|
656
|
+
const userPrompt = buildHandlerPrompt("", plannedTool, contents);
|
|
657
|
+
let lastError = null;
|
|
658
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
659
|
+
let response;
|
|
660
|
+
try {
|
|
661
|
+
response = await llm.generate(systemPrompt, userPrompt);
|
|
662
|
+
} catch (error) {
|
|
663
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
664
|
+
throw new Error(
|
|
665
|
+
`LLM error while compiling "${plannedTool.name}": ${message}`
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
const code = extractCode(response);
|
|
669
|
+
try {
|
|
670
|
+
validateCode(code);
|
|
671
|
+
} catch (error) {
|
|
672
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
673
|
+
if (attempt === 0) {
|
|
674
|
+
warn(
|
|
675
|
+
`Invalid code for "${plannedTool.name}" (${lastError.message}), retrying...`
|
|
676
|
+
);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
throw new Error(
|
|
680
|
+
`Failed to compile handler for "${plannedTool.name}": ${lastError.message}`
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
tools.set(plannedTool.name, {
|
|
684
|
+
name: plannedTool.name,
|
|
685
|
+
description: plannedTool.description,
|
|
686
|
+
input_schema: plannedTool.input_schema,
|
|
687
|
+
handler_code: code,
|
|
688
|
+
needs_network: plannedTool.needs_network
|
|
689
|
+
});
|
|
690
|
+
verbose(`Compiled handler for "${plannedTool.name}" (${code.length} chars)`);
|
|
691
|
+
break;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return { tools, whitelist_domains: [] };
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// src/engine/executor.ts
|
|
698
|
+
function createExecutor(compiled, sandbox) {
|
|
699
|
+
return {
|
|
700
|
+
async execute(toolName, args) {
|
|
701
|
+
const tool = compiled.tools.get(toolName);
|
|
702
|
+
if (!tool) {
|
|
703
|
+
return {
|
|
704
|
+
content: [{ type: "text", text: `Unknown tool: ${toolName}` }],
|
|
705
|
+
isError: true
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
return await sandbox.runHandler(tool.handler_code, args);
|
|
710
|
+
} catch (error) {
|
|
711
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
712
|
+
return {
|
|
713
|
+
content: [{ type: "text", text: `Handler error: ${message}` }],
|
|
714
|
+
isError: true
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
},
|
|
718
|
+
getExposedTools() {
|
|
719
|
+
return Array.from(compiled.tools.values()).map((tool) => ({
|
|
720
|
+
name: tool.name,
|
|
721
|
+
description: tool.description,
|
|
722
|
+
inputSchema: tool.input_schema
|
|
723
|
+
}));
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// src/engine/sandbox.ts
|
|
729
|
+
import vm from "node:vm";
|
|
730
|
+
var HANDLER_TIMEOUT_MS = 3e4;
|
|
731
|
+
function createSandbox(whitelistedFetch, timeoutMs = HANDLER_TIMEOUT_MS) {
|
|
732
|
+
return {
|
|
733
|
+
async runHandler(code, args) {
|
|
734
|
+
const context = vm.createContext({
|
|
735
|
+
// Safe data-manipulation globals
|
|
736
|
+
JSON,
|
|
737
|
+
Math,
|
|
738
|
+
String,
|
|
739
|
+
Number,
|
|
740
|
+
Boolean,
|
|
741
|
+
Array,
|
|
742
|
+
Object,
|
|
743
|
+
Map,
|
|
744
|
+
Set,
|
|
745
|
+
Date,
|
|
746
|
+
RegExp,
|
|
747
|
+
parseInt,
|
|
748
|
+
parseFloat,
|
|
749
|
+
isNaN,
|
|
750
|
+
isFinite,
|
|
751
|
+
structuredClone,
|
|
752
|
+
Promise,
|
|
753
|
+
// URL handling
|
|
754
|
+
URL,
|
|
755
|
+
URLSearchParams,
|
|
756
|
+
TextEncoder,
|
|
757
|
+
TextDecoder,
|
|
758
|
+
Headers,
|
|
759
|
+
Response,
|
|
760
|
+
// Network access (whitelisted)
|
|
761
|
+
fetch: whitelistedFetch,
|
|
762
|
+
// Logging (redirected to stderr)
|
|
763
|
+
console: {
|
|
764
|
+
log: (...logArgs) => console.error("[sandbox]", ...logArgs)
|
|
765
|
+
},
|
|
766
|
+
// Injected per-call
|
|
767
|
+
inputArgs: structuredClone(args),
|
|
768
|
+
fetchFn: whitelistedFetch
|
|
769
|
+
});
|
|
770
|
+
const wrappedCode = `(async function(args, fetch) { ${code} })(inputArgs, fetchFn)`;
|
|
771
|
+
const script = new vm.Script(wrappedCode);
|
|
772
|
+
const resultPromise = script.runInContext(context, {
|
|
773
|
+
timeout: timeoutMs
|
|
774
|
+
});
|
|
775
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
776
|
+
setTimeout(
|
|
777
|
+
() => reject(new Error("Handler timed out")),
|
|
778
|
+
timeoutMs
|
|
779
|
+
);
|
|
780
|
+
});
|
|
781
|
+
const result = await Promise.race([resultPromise, timeoutPromise]);
|
|
782
|
+
if (!result || !Array.isArray(result.content)) {
|
|
783
|
+
throw new Error("Handler must return {content: [...]}");
|
|
784
|
+
}
|
|
785
|
+
return result;
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
// src/server.ts
|
|
791
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
792
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
793
|
+
import {
|
|
794
|
+
ListToolsRequestSchema,
|
|
795
|
+
CallToolRequestSchema
|
|
796
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
797
|
+
import http from "node:http";
|
|
798
|
+
function readBody(req) {
|
|
799
|
+
return new Promise((resolve, reject) => {
|
|
800
|
+
const chunks = [];
|
|
801
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
802
|
+
req.on("end", () => {
|
|
803
|
+
try {
|
|
804
|
+
const body = JSON.parse(Buffer.concat(chunks).toString());
|
|
805
|
+
resolve(body);
|
|
806
|
+
} catch (e) {
|
|
807
|
+
reject(e);
|
|
808
|
+
}
|
|
809
|
+
});
|
|
810
|
+
req.on("error", reject);
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
function createExposedServer(config, executor) {
|
|
814
|
+
const httpServer = http.createServer(async (req, res) => {
|
|
815
|
+
if (req.method === "POST" && req.url === "/mcp") {
|
|
816
|
+
const mcpServer = new Server(
|
|
817
|
+
{ name: "mcpboot", version: "0.1.0" },
|
|
818
|
+
{ capabilities: { tools: {} } }
|
|
819
|
+
);
|
|
820
|
+
mcpServer.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
821
|
+
const tools = executor.getExposedTools();
|
|
822
|
+
return {
|
|
823
|
+
tools: tools.map((t) => ({
|
|
824
|
+
name: t.name,
|
|
825
|
+
description: t.description,
|
|
826
|
+
inputSchema: t.inputSchema
|
|
827
|
+
}))
|
|
828
|
+
};
|
|
829
|
+
});
|
|
830
|
+
mcpServer.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
831
|
+
const { name, arguments: args } = request.params;
|
|
832
|
+
return executor.execute(name, args ?? {});
|
|
833
|
+
});
|
|
834
|
+
const transport = new StreamableHTTPServerTransport({
|
|
835
|
+
sessionIdGenerator: void 0
|
|
836
|
+
});
|
|
837
|
+
try {
|
|
838
|
+
const body = await readBody(req);
|
|
839
|
+
await mcpServer.connect(transport);
|
|
840
|
+
await transport.handleRequest(req, res, body);
|
|
841
|
+
res.on("close", () => {
|
|
842
|
+
transport.close();
|
|
843
|
+
mcpServer.close();
|
|
844
|
+
});
|
|
845
|
+
} catch (error) {
|
|
846
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
847
|
+
warn(`HTTP request error: ${message}`);
|
|
848
|
+
if (!res.headersSent) {
|
|
849
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
850
|
+
res.end(
|
|
851
|
+
JSON.stringify({
|
|
852
|
+
jsonrpc: "2.0",
|
|
853
|
+
error: { code: -32603, message: "Internal server error" },
|
|
854
|
+
id: null
|
|
855
|
+
})
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
} else if (req.method === "GET" && req.url === "/health") {
|
|
860
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
861
|
+
res.end(
|
|
862
|
+
JSON.stringify({
|
|
863
|
+
status: "ok",
|
|
864
|
+
tools: executor.getExposedTools().length
|
|
865
|
+
})
|
|
866
|
+
);
|
|
867
|
+
} else {
|
|
868
|
+
res.writeHead(404);
|
|
869
|
+
res.end("Not found");
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
return {
|
|
873
|
+
start() {
|
|
874
|
+
return new Promise((resolve, reject) => {
|
|
875
|
+
httpServer.on("error", reject);
|
|
876
|
+
httpServer.listen(config.port, () => {
|
|
877
|
+
const addr = httpServer.address();
|
|
878
|
+
const actualPort = typeof addr === "object" && addr !== null ? addr.port : config.port;
|
|
879
|
+
resolve(actualPort);
|
|
880
|
+
});
|
|
881
|
+
});
|
|
882
|
+
},
|
|
883
|
+
stop() {
|
|
884
|
+
return new Promise((resolve, reject) => {
|
|
885
|
+
httpServer.close((err) => {
|
|
886
|
+
if (err) reject(err);
|
|
887
|
+
else resolve();
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// src/index.ts
|
|
895
|
+
function buildContentHash(contents) {
|
|
896
|
+
const sorted = [...contents].sort((a, b) => a.url.localeCompare(b.url));
|
|
897
|
+
const joined = sorted.map((c) => c.content).join("\n---\n");
|
|
898
|
+
return hash(joined);
|
|
899
|
+
}
|
|
900
|
+
function reconstructWhitelist(domains) {
|
|
901
|
+
const domainSet = new Set(domains);
|
|
902
|
+
return {
|
|
903
|
+
domains: domainSet,
|
|
904
|
+
allows(url) {
|
|
905
|
+
let hostname;
|
|
906
|
+
try {
|
|
907
|
+
hostname = new URL(url).hostname;
|
|
908
|
+
} catch {
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
for (const d of domainSet) {
|
|
912
|
+
if (hostname === d || hostname.endsWith("." + d)) return true;
|
|
913
|
+
}
|
|
914
|
+
return false;
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
async function main(argv = process.argv) {
|
|
919
|
+
const config = buildConfig(argv);
|
|
920
|
+
if (!config) return;
|
|
921
|
+
setVerbose(config.verbose);
|
|
922
|
+
const urls = extractUrls(config.prompt);
|
|
923
|
+
log(`Found ${urls.length} URL(s) in prompt`);
|
|
924
|
+
const contents = await fetchUrls(urls);
|
|
925
|
+
log(`Fetched ${contents.length} page(s)`);
|
|
926
|
+
if (urls.length > 0 && contents.length === 0) {
|
|
927
|
+
warn(
|
|
928
|
+
"All URL fetches failed. Proceeding with prompt text only \u2014 generated tools may be less accurate"
|
|
929
|
+
);
|
|
930
|
+
}
|
|
931
|
+
const whitelist = buildWhitelist(urls, contents);
|
|
932
|
+
const whitelistDomains = [...whitelist.domains];
|
|
933
|
+
verbose(`Whitelist: ${whitelistDomains.join(", ") || "(empty)"}`);
|
|
934
|
+
const cache = createCache(config.cache);
|
|
935
|
+
const promptHash = hash(config.prompt);
|
|
936
|
+
const contentHash = buildContentHash(contents);
|
|
937
|
+
let compiled;
|
|
938
|
+
let activeWhitelist;
|
|
939
|
+
const cached = cache.get(promptHash, contentHash);
|
|
940
|
+
if (cached) {
|
|
941
|
+
log("Cache hit \u2014 loading generated tools");
|
|
942
|
+
compiled = deserializeCompiled(cached);
|
|
943
|
+
activeWhitelist = reconstructWhitelist(cached.whitelist_domains);
|
|
944
|
+
} else {
|
|
945
|
+
log("Cache miss \u2014 generating tools via LLM");
|
|
946
|
+
const llm = createLLMClient(config.llm);
|
|
947
|
+
const plan = await generatePlan(llm, config.prompt, contents, whitelist);
|
|
948
|
+
log(`Plan: ${plan.tools.length} tool(s)`);
|
|
949
|
+
if (config.dryRun) {
|
|
950
|
+
console.log(JSON.stringify(plan, null, 2));
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
compiled = await compilePlan(llm, plan, contents);
|
|
954
|
+
compiled.whitelist_domains = whitelistDomains;
|
|
955
|
+
log(`Compiled ${compiled.tools.size} handler(s)`);
|
|
956
|
+
const { compiledTools } = serializeCompiled(compiled);
|
|
957
|
+
cache.set({
|
|
958
|
+
promptHash,
|
|
959
|
+
contentHash,
|
|
960
|
+
plan,
|
|
961
|
+
compiledTools,
|
|
962
|
+
whitelist_domains: whitelistDomains,
|
|
963
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
964
|
+
});
|
|
965
|
+
activeWhitelist = whitelist;
|
|
966
|
+
}
|
|
967
|
+
if (config.dryRun) {
|
|
968
|
+
log(`${compiled.tools.size} cached tool(s) available`);
|
|
969
|
+
const toolNames = Array.from(compiled.tools.keys());
|
|
970
|
+
console.log(JSON.stringify({ cached: true, tools: toolNames }, null, 2));
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
const whitelistedFetch = createWhitelistedFetch(activeWhitelist);
|
|
974
|
+
const sandbox = createSandbox(whitelistedFetch);
|
|
975
|
+
const executor = createExecutor(compiled, sandbox);
|
|
976
|
+
const server = createExposedServer(config.server, executor);
|
|
977
|
+
const port = await server.start();
|
|
978
|
+
log(`Listening on http://localhost:${port}/mcp`);
|
|
979
|
+
log(`Serving ${executor.getExposedTools().length} tool(s)`);
|
|
980
|
+
const shutdown = async () => {
|
|
981
|
+
log("Shutting down...");
|
|
982
|
+
await server.stop();
|
|
983
|
+
process.exit(0);
|
|
984
|
+
};
|
|
985
|
+
process.on("SIGINT", shutdown);
|
|
986
|
+
process.on("SIGTERM", shutdown);
|
|
987
|
+
}
|
|
988
|
+
if (process.env.VITEST !== "true") {
|
|
989
|
+
main().catch((error) => {
|
|
990
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
991
|
+
console.error(`[mcpboot] Fatal: ${message}`);
|
|
992
|
+
process.exit(1);
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
export {
|
|
996
|
+
main
|
|
997
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mcpboot",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generate and serve an MCP server from a natural language prompt",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mcpboot": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js --packages=external --banner:js='#!/usr/bin/env node'",
|
|
14
|
+
"dev": "tsx src/index.ts",
|
|
15
|
+
"test": "vitest run",
|
|
16
|
+
"test:watch": "vitest",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"mcp",
|
|
21
|
+
"server",
|
|
22
|
+
"llm",
|
|
23
|
+
"tools",
|
|
24
|
+
"generate"
|
|
25
|
+
],
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@ai-sdk/anthropic": "^1.2.12",
|
|
32
|
+
"@ai-sdk/openai": "^1.3.22",
|
|
33
|
+
"@modelcontextprotocol/sdk": "^1.12.1",
|
|
34
|
+
"ai": "^4.3.16",
|
|
35
|
+
"commander": "^13.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^22.13.1",
|
|
39
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
40
|
+
"esbuild": "^0.25.0",
|
|
41
|
+
"tsx": "^4.19.3",
|
|
42
|
+
"typescript": "^5.7.3",
|
|
43
|
+
"vitest": "^3.0.5"
|
|
44
|
+
}
|
|
45
|
+
}
|