@drumcode/runner 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 +139 -0
- package/dist/chunk-3CUTJWRW.mjs +358 -0
- package/dist/chunk-4ITFFQXW.mjs +716 -0
- package/dist/chunk-5NEEOHT4.mjs +632 -0
- package/dist/chunk-6BC7SCK5.mjs +720 -0
- package/dist/chunk-E3F3IKO5.mjs +359 -0
- package/dist/chunk-JN5YMFB7.mjs +1061 -0
- package/dist/chunk-T7I5AI3A.mjs +558 -0
- package/dist/chunk-VDJVZU3Q.mjs +908 -0
- package/dist/chunk-VYYR5EI4.mjs +1061 -0
- package/dist/chunk-ZOZS5KN7.mjs +713 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1432 -0
- package/dist/cli.mjs +360 -0
- package/dist/index.d.mts +192 -0
- package/dist/index.d.ts +192 -0
- package/dist/index.js +1085 -0
- package/dist/index.mjs +8 -0
- package/package.json +56 -0
|
@@ -0,0 +1,713 @@
|
|
|
1
|
+
// src/runner.ts
|
|
2
|
+
import { CORE_TOOLSET, redactSecrets } from "@drumcode/core";
|
|
3
|
+
var DrumcodeRunner = class {
|
|
4
|
+
options;
|
|
5
|
+
toolCache = /* @__PURE__ */ new Map();
|
|
6
|
+
manifestCache = null;
|
|
7
|
+
authContext = {
|
|
8
|
+
isAuthenticated: false,
|
|
9
|
+
projectId: null,
|
|
10
|
+
tokenId: null,
|
|
11
|
+
tokenName: null,
|
|
12
|
+
profileId: null
|
|
13
|
+
};
|
|
14
|
+
authInitialized = false;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.log("info", "Drumcode Runner initialized", {
|
|
18
|
+
registryUrl: options.registryUrl,
|
|
19
|
+
cacheEnabled: options.cacheEnabled
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Authentication
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
/**
|
|
26
|
+
* Initialize authentication by validating the token with the registry
|
|
27
|
+
* This should be called before processing any requests
|
|
28
|
+
*/
|
|
29
|
+
async initializeAuth() {
|
|
30
|
+
if (this.authInitialized) {
|
|
31
|
+
return this.authContext;
|
|
32
|
+
}
|
|
33
|
+
if (!this.options.token) {
|
|
34
|
+
this.log("info", "No token provided, running in demo mode");
|
|
35
|
+
this.authInitialized = true;
|
|
36
|
+
return this.authContext;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
this.log("debug", "Validating token with registry");
|
|
40
|
+
const response = await fetch(`${this.options.registryUrl}/v1/auth/token`, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: this.getAuthHeaders()
|
|
43
|
+
});
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const errorData = await response.json().catch(() => ({}));
|
|
46
|
+
this.log("warn", "Token validation failed", {
|
|
47
|
+
status: response.status,
|
|
48
|
+
error: errorData.error?.message
|
|
49
|
+
});
|
|
50
|
+
this.authInitialized = true;
|
|
51
|
+
return this.authContext;
|
|
52
|
+
}
|
|
53
|
+
const data = await response.json();
|
|
54
|
+
if (data.valid && data.project && data.token) {
|
|
55
|
+
this.authContext = {
|
|
56
|
+
isAuthenticated: true,
|
|
57
|
+
projectId: data.project.id,
|
|
58
|
+
tokenId: data.token.id,
|
|
59
|
+
tokenName: data.token.name,
|
|
60
|
+
profileId: data.token.profileId
|
|
61
|
+
};
|
|
62
|
+
this.log("info", "Token validated successfully", {
|
|
63
|
+
projectId: this.authContext.projectId,
|
|
64
|
+
tokenName: this.authContext.tokenName,
|
|
65
|
+
hasProfile: !!this.authContext.profileId
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
this.log("error", "Token validation error", {
|
|
70
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
this.authInitialized = true;
|
|
74
|
+
return this.authContext;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get the current authentication context
|
|
78
|
+
*/
|
|
79
|
+
getAuthContext() {
|
|
80
|
+
return this.authContext;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check if the runner is authenticated
|
|
84
|
+
*/
|
|
85
|
+
isAuthenticated() {
|
|
86
|
+
return this.authContext.isAuthenticated;
|
|
87
|
+
}
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// Tool Discovery
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
/**
|
|
92
|
+
* Get the list of tools to expose to the client
|
|
93
|
+
* Uses Polyfill strategy for non-Anthropic clients
|
|
94
|
+
*/
|
|
95
|
+
async getToolList(capabilities) {
|
|
96
|
+
if (capabilities.supportsAdvancedToolUse && capabilities.supportsDeferredLoading) {
|
|
97
|
+
this.log("debug", "Using Strategy A: Full manifest with deferred loading");
|
|
98
|
+
return this.getFullManifest();
|
|
99
|
+
}
|
|
100
|
+
this.log("debug", "Using Strategy B: Polyfill mode with core toolset");
|
|
101
|
+
return CORE_TOOLSET;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Fetch the full tool manifest from the Registry
|
|
105
|
+
*/
|
|
106
|
+
async getFullManifest() {
|
|
107
|
+
if (this.manifestCache && this.options.cacheEnabled) {
|
|
108
|
+
return this.manifestCache;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const response = await fetch(`${this.options.registryUrl}/v1/manifest`, {
|
|
112
|
+
headers: this.getAuthHeaders()
|
|
113
|
+
});
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
throw new Error(`Registry returned ${response.status}`);
|
|
116
|
+
}
|
|
117
|
+
const data = await response.json();
|
|
118
|
+
this.manifestCache = data.tools;
|
|
119
|
+
return this.manifestCache;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
this.log("warn", "Failed to fetch manifest, using cached or fallback", { error });
|
|
122
|
+
return this.manifestCache || CORE_TOOLSET;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
// Tool Execution
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
/**
|
|
129
|
+
* Execute a tool call
|
|
130
|
+
*/
|
|
131
|
+
async executeTool(request) {
|
|
132
|
+
const startTime = Date.now();
|
|
133
|
+
try {
|
|
134
|
+
if (request.name === "drumcode_search_tools") {
|
|
135
|
+
return this.handleSearchTools(request.arguments);
|
|
136
|
+
}
|
|
137
|
+
if (request.name === "drumcode_get_tool_schema") {
|
|
138
|
+
return this.handleGetToolSchema(request.arguments);
|
|
139
|
+
}
|
|
140
|
+
return this.executeRegularTool(request);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
const latency = Date.now() - startTime;
|
|
143
|
+
this.log("error", "Tool execution failed", {
|
|
144
|
+
tool: request.name,
|
|
145
|
+
error,
|
|
146
|
+
latencyMs: latency
|
|
147
|
+
});
|
|
148
|
+
return {
|
|
149
|
+
content: [{
|
|
150
|
+
type: "text",
|
|
151
|
+
text: `Error executing tool: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
152
|
+
}],
|
|
153
|
+
isError: true
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Handle drumcode_search_tools meta-tool
|
|
159
|
+
*/
|
|
160
|
+
async handleSearchTools(args) {
|
|
161
|
+
this.log("debug", "Searching tools", { query: args.query });
|
|
162
|
+
try {
|
|
163
|
+
const response = await fetch(`${this.options.registryUrl}/v1/tools/search`, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers: {
|
|
166
|
+
...this.getAuthHeaders(),
|
|
167
|
+
"Content-Type": "application/json"
|
|
168
|
+
},
|
|
169
|
+
body: JSON.stringify({
|
|
170
|
+
query: args.query,
|
|
171
|
+
project_token: this.options.token,
|
|
172
|
+
limit: 5
|
|
173
|
+
})
|
|
174
|
+
});
|
|
175
|
+
if (!response.ok) {
|
|
176
|
+
throw new Error(`Search failed with status ${response.status}`);
|
|
177
|
+
}
|
|
178
|
+
const data = await response.json();
|
|
179
|
+
const resultText = data.matches.length > 0 ? data.matches.map(
|
|
180
|
+
(m, i) => `${i + 1}. **${m.name}** (relevance: ${(m.relevance * 100).toFixed(0)}%)
|
|
181
|
+
${m.description}`
|
|
182
|
+
).join("\n\n") : "No matching tools found. Try a different search query.";
|
|
183
|
+
return {
|
|
184
|
+
content: [{
|
|
185
|
+
type: "text",
|
|
186
|
+
text: `Found ${data.matches.length} matching tools:
|
|
187
|
+
|
|
188
|
+
${resultText}
|
|
189
|
+
|
|
190
|
+
Use \`drumcode_get_tool_schema\` to get detailed arguments for any of these tools.`
|
|
191
|
+
}]
|
|
192
|
+
};
|
|
193
|
+
} catch (error) {
|
|
194
|
+
this.log("error", "Search failed", { error });
|
|
195
|
+
return {
|
|
196
|
+
content: [{
|
|
197
|
+
type: "text",
|
|
198
|
+
text: `Search failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
199
|
+
}],
|
|
200
|
+
isError: true
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Handle drumcode_get_tool_schema meta-tool
|
|
206
|
+
*/
|
|
207
|
+
async handleGetToolSchema(args) {
|
|
208
|
+
this.log("debug", "Fetching tool schema", { toolName: args.tool_name });
|
|
209
|
+
if (this.toolCache.has(args.tool_name)) {
|
|
210
|
+
const cached = this.toolCache.get(args.tool_name);
|
|
211
|
+
return {
|
|
212
|
+
content: [{
|
|
213
|
+
type: "text",
|
|
214
|
+
text: this.formatToolSchema(cached)
|
|
215
|
+
}]
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
const response = await fetch(
|
|
220
|
+
`${this.options.registryUrl}/v1/tools/${encodeURIComponent(args.tool_name)}`,
|
|
221
|
+
{ headers: this.getAuthHeaders() }
|
|
222
|
+
);
|
|
223
|
+
if (!response.ok) {
|
|
224
|
+
if (response.status === 404) {
|
|
225
|
+
return {
|
|
226
|
+
content: [{
|
|
227
|
+
type: "text",
|
|
228
|
+
text: `Tool "${args.tool_name}" not found. Use drumcode_search_tools to find available tools.`
|
|
229
|
+
}],
|
|
230
|
+
isError: true
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
throw new Error(`Failed to fetch schema: ${response.status}`);
|
|
234
|
+
}
|
|
235
|
+
const schema = await response.json();
|
|
236
|
+
const tool = {
|
|
237
|
+
name: schema.name,
|
|
238
|
+
description: schema.description,
|
|
239
|
+
input_schema: schema.input_schema
|
|
240
|
+
};
|
|
241
|
+
this.toolCache.set(args.tool_name, tool);
|
|
242
|
+
return {
|
|
243
|
+
content: [{
|
|
244
|
+
type: "text",
|
|
245
|
+
text: this.formatToolSchema(tool)
|
|
246
|
+
}]
|
|
247
|
+
};
|
|
248
|
+
} catch (error) {
|
|
249
|
+
this.log("error", "Schema fetch failed", { error });
|
|
250
|
+
return {
|
|
251
|
+
content: [{
|
|
252
|
+
type: "text",
|
|
253
|
+
text: `Failed to fetch schema: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
254
|
+
}],
|
|
255
|
+
isError: true
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Execute a regular (non-meta) tool
|
|
261
|
+
*/
|
|
262
|
+
async executeRegularTool(request) {
|
|
263
|
+
if (request.name === "drumcode_echo") {
|
|
264
|
+
return {
|
|
265
|
+
content: [{
|
|
266
|
+
type: "text",
|
|
267
|
+
text: JSON.stringify(request.arguments, null, 2)
|
|
268
|
+
}]
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (request.name === "drumcode_http_get") {
|
|
272
|
+
const url = request.arguments.url;
|
|
273
|
+
try {
|
|
274
|
+
const response = await fetch(url);
|
|
275
|
+
const text = await response.text();
|
|
276
|
+
return {
|
|
277
|
+
content: [{
|
|
278
|
+
type: "text",
|
|
279
|
+
text: `Status: ${response.status}
|
|
280
|
+
|
|
281
|
+
${text.slice(0, 5e3)}${text.length > 5e3 ? "...(truncated)" : ""}`
|
|
282
|
+
}]
|
|
283
|
+
};
|
|
284
|
+
} catch (error) {
|
|
285
|
+
return {
|
|
286
|
+
content: [{
|
|
287
|
+
type: "text",
|
|
288
|
+
text: `HTTP request failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
289
|
+
}],
|
|
290
|
+
isError: true
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (request.name.startsWith("arxiv_")) {
|
|
295
|
+
return this.executeArxivTool(request);
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
content: [{
|
|
299
|
+
type: "text",
|
|
300
|
+
text: `Tool "${request.name}" is not yet implemented. This tool needs to be fetched and executed via the Registry.`
|
|
301
|
+
}],
|
|
302
|
+
isError: true
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Execute arXiv tools via the arXiv API
|
|
307
|
+
*/
|
|
308
|
+
async executeArxivTool(request) {
|
|
309
|
+
const baseUrl = "http://export.arxiv.org/api/query";
|
|
310
|
+
try {
|
|
311
|
+
switch (request.name) {
|
|
312
|
+
case "arxiv_search_papers": {
|
|
313
|
+
const { query, max_results = 10, categories } = request.arguments;
|
|
314
|
+
let searchQuery = query;
|
|
315
|
+
if (categories && categories.length > 0) {
|
|
316
|
+
const catFilter = categories.map((c) => `cat:${c}`).join(" OR ");
|
|
317
|
+
searchQuery = `(${query}) AND (${catFilter})`;
|
|
318
|
+
}
|
|
319
|
+
const params = new URLSearchParams({
|
|
320
|
+
search_query: `all:${searchQuery}`,
|
|
321
|
+
start: "0",
|
|
322
|
+
max_results: String(Math.min(max_results, 20)),
|
|
323
|
+
sortBy: "submittedDate",
|
|
324
|
+
sortOrder: "descending"
|
|
325
|
+
});
|
|
326
|
+
const response = await fetch(`${baseUrl}?${params}`);
|
|
327
|
+
const xml = await response.text();
|
|
328
|
+
const papers = this.parseArxivResponse(xml);
|
|
329
|
+
return {
|
|
330
|
+
content: [{
|
|
331
|
+
type: "text",
|
|
332
|
+
text: `Found ${papers.length} papers:
|
|
333
|
+
|
|
334
|
+
${papers.map(
|
|
335
|
+
(p, i) => `${i + 1}. **${p.title}**
|
|
336
|
+
ID: ${p.id}
|
|
337
|
+
Authors: ${p.authors}
|
|
338
|
+
Published: ${p.published}
|
|
339
|
+
${p.summary.slice(0, 200)}...`
|
|
340
|
+
).join("\n\n")}`
|
|
341
|
+
}]
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
case "arxiv_get_paper":
|
|
345
|
+
case "arxiv_get_abstract": {
|
|
346
|
+
const { paper_id } = request.arguments;
|
|
347
|
+
const params = new URLSearchParams({ id_list: paper_id });
|
|
348
|
+
const response = await fetch(`${baseUrl}?${params}`);
|
|
349
|
+
const xml = await response.text();
|
|
350
|
+
const papers = this.parseArxivResponse(xml);
|
|
351
|
+
if (papers.length === 0) {
|
|
352
|
+
return {
|
|
353
|
+
content: [{ type: "text", text: `Paper ${paper_id} not found.` }],
|
|
354
|
+
isError: true
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
const p = papers[0];
|
|
358
|
+
return {
|
|
359
|
+
content: [{
|
|
360
|
+
type: "text",
|
|
361
|
+
text: `**${p.title}**
|
|
362
|
+
|
|
363
|
+
ID: ${p.id}
|
|
364
|
+
Authors: ${p.authors}
|
|
365
|
+
Published: ${p.published}
|
|
366
|
+
Categories: ${p.categories}
|
|
367
|
+
PDF: ${p.pdf_url}
|
|
368
|
+
|
|
369
|
+
**Abstract:**
|
|
370
|
+
${p.summary}`
|
|
371
|
+
}]
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
case "arxiv_search_by_author": {
|
|
375
|
+
const { author, max_results = 10 } = request.arguments;
|
|
376
|
+
const params = new URLSearchParams({
|
|
377
|
+
search_query: `au:${author}`,
|
|
378
|
+
start: "0",
|
|
379
|
+
max_results: String(Math.min(max_results, 20)),
|
|
380
|
+
sortBy: "submittedDate",
|
|
381
|
+
sortOrder: "descending"
|
|
382
|
+
});
|
|
383
|
+
const response = await fetch(`${baseUrl}?${params}`);
|
|
384
|
+
const xml = await response.text();
|
|
385
|
+
const papers = this.parseArxivResponse(xml);
|
|
386
|
+
return {
|
|
387
|
+
content: [{
|
|
388
|
+
type: "text",
|
|
389
|
+
text: `Found ${papers.length} papers by "${author}":
|
|
390
|
+
|
|
391
|
+
${papers.map(
|
|
392
|
+
(p, i) => `${i + 1}. **${p.title}** (${p.id})
|
|
393
|
+
${p.published}`
|
|
394
|
+
).join("\n\n")}`
|
|
395
|
+
}]
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
case "arxiv_search_by_category": {
|
|
399
|
+
const { category, max_results = 10 } = request.arguments;
|
|
400
|
+
const params = new URLSearchParams({
|
|
401
|
+
search_query: `cat:${category}`,
|
|
402
|
+
start: "0",
|
|
403
|
+
max_results: String(Math.min(max_results, 20)),
|
|
404
|
+
sortBy: "submittedDate",
|
|
405
|
+
sortOrder: "descending"
|
|
406
|
+
});
|
|
407
|
+
const response = await fetch(`${baseUrl}?${params}`);
|
|
408
|
+
const xml = await response.text();
|
|
409
|
+
const papers = this.parseArxivResponse(xml);
|
|
410
|
+
return {
|
|
411
|
+
content: [{
|
|
412
|
+
type: "text",
|
|
413
|
+
text: `Found ${papers.length} recent papers in ${category}:
|
|
414
|
+
|
|
415
|
+
${papers.map(
|
|
416
|
+
(p, i) => `${i + 1}. **${p.title}** (${p.id})
|
|
417
|
+
Authors: ${p.authors}
|
|
418
|
+
${p.published}`
|
|
419
|
+
).join("\n\n")}`
|
|
420
|
+
}]
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
default:
|
|
424
|
+
return {
|
|
425
|
+
content: [{
|
|
426
|
+
type: "text",
|
|
427
|
+
text: `arXiv tool "${request.name}" is not yet implemented.`
|
|
428
|
+
}],
|
|
429
|
+
isError: true
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
} catch (error) {
|
|
433
|
+
this.log("error", "arXiv API call failed", { error });
|
|
434
|
+
return {
|
|
435
|
+
content: [{
|
|
436
|
+
type: "text",
|
|
437
|
+
text: `arXiv API error: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
438
|
+
}],
|
|
439
|
+
isError: true
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Parse arXiv Atom XML response
|
|
445
|
+
*/
|
|
446
|
+
parseArxivResponse(xml) {
|
|
447
|
+
const papers = [];
|
|
448
|
+
const entryRegex = /<entry>([\s\S]*?)<\/entry>/g;
|
|
449
|
+
let match;
|
|
450
|
+
while ((match = entryRegex.exec(xml)) !== null) {
|
|
451
|
+
const entry = match[1];
|
|
452
|
+
const getId = (s) => {
|
|
453
|
+
const m = s.match(/<id>(.*?)<\/id>/);
|
|
454
|
+
return m ? m[1].replace("http://arxiv.org/abs/", "") : "";
|
|
455
|
+
};
|
|
456
|
+
const getTitle = (s) => {
|
|
457
|
+
const m = s.match(/<title>([\s\S]*?)<\/title>/);
|
|
458
|
+
return m ? m[1].replace(/\s+/g, " ").trim() : "";
|
|
459
|
+
};
|
|
460
|
+
const getSummary = (s) => {
|
|
461
|
+
const m = s.match(/<summary>([\s\S]*?)<\/summary>/);
|
|
462
|
+
return m ? m[1].replace(/\s+/g, " ").trim() : "";
|
|
463
|
+
};
|
|
464
|
+
const getAuthors = (s) => {
|
|
465
|
+
const authors = [];
|
|
466
|
+
const authorRegex = /<author>\s*<name>(.*?)<\/name>/g;
|
|
467
|
+
let authorMatch;
|
|
468
|
+
while ((authorMatch = authorRegex.exec(s)) !== null) {
|
|
469
|
+
authors.push(authorMatch[1]);
|
|
470
|
+
}
|
|
471
|
+
return authors.join(", ");
|
|
472
|
+
};
|
|
473
|
+
const getPublished = (s) => {
|
|
474
|
+
const m = s.match(/<published>(.*?)<\/published>/);
|
|
475
|
+
return m ? m[1].split("T")[0] : "";
|
|
476
|
+
};
|
|
477
|
+
const getCategories = (s) => {
|
|
478
|
+
const cats = [];
|
|
479
|
+
const catRegex = /<category[^>]*term="([^"]+)"/g;
|
|
480
|
+
let catMatch;
|
|
481
|
+
while ((catMatch = catRegex.exec(s)) !== null) {
|
|
482
|
+
cats.push(catMatch[1]);
|
|
483
|
+
}
|
|
484
|
+
return cats.join(", ");
|
|
485
|
+
};
|
|
486
|
+
const getPdfUrl = (s) => {
|
|
487
|
+
const m = s.match(/<link[^>]*title="pdf"[^>]*href="([^"]+)"/);
|
|
488
|
+
return m ? m[1] : "";
|
|
489
|
+
};
|
|
490
|
+
papers.push({
|
|
491
|
+
id: getId(entry),
|
|
492
|
+
title: getTitle(entry),
|
|
493
|
+
summary: getSummary(entry),
|
|
494
|
+
authors: getAuthors(entry),
|
|
495
|
+
published: getPublished(entry),
|
|
496
|
+
categories: getCategories(entry),
|
|
497
|
+
pdf_url: getPdfUrl(entry)
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
return papers;
|
|
501
|
+
}
|
|
502
|
+
// ---------------------------------------------------------------------------
|
|
503
|
+
// Helpers
|
|
504
|
+
// ---------------------------------------------------------------------------
|
|
505
|
+
getAuthHeaders() {
|
|
506
|
+
const headers = {
|
|
507
|
+
"User-Agent": "drumcode-runner/0.1.0"
|
|
508
|
+
};
|
|
509
|
+
if (this.options.token) {
|
|
510
|
+
headers["Authorization"] = `Bearer ${this.options.token}`;
|
|
511
|
+
}
|
|
512
|
+
return headers;
|
|
513
|
+
}
|
|
514
|
+
formatToolSchema(tool) {
|
|
515
|
+
const props = tool.input_schema.properties;
|
|
516
|
+
const required = tool.input_schema.required || [];
|
|
517
|
+
const argsDescription = Object.entries(props).map(([name, prop]) => {
|
|
518
|
+
const req = required.includes(name) ? " (required)" : " (optional)";
|
|
519
|
+
return ` - ${name}${req}: ${prop.type}${prop.description ? ` - ${prop.description}` : ""}`;
|
|
520
|
+
}).join("\n");
|
|
521
|
+
return `## ${tool.name}
|
|
522
|
+
|
|
523
|
+
${tool.description}
|
|
524
|
+
|
|
525
|
+
### Arguments:
|
|
526
|
+
${argsDescription || " No arguments required."}
|
|
527
|
+
|
|
528
|
+
### Usage:
|
|
529
|
+
Call this tool with the arguments above to execute it.`;
|
|
530
|
+
}
|
|
531
|
+
log(level, message, data) {
|
|
532
|
+
const levels = ["debug", "info", "warn", "error"];
|
|
533
|
+
const currentLevel = levels.indexOf(this.options.logLevel);
|
|
534
|
+
const messageLevel = levels.indexOf(level);
|
|
535
|
+
if (messageLevel >= currentLevel) {
|
|
536
|
+
const safeData = data ? JSON.parse(redactSecrets(JSON.stringify(data))) : void 0;
|
|
537
|
+
console.error(`[drumcode] ${message}`, safeData ? safeData : "");
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// src/server.ts
|
|
543
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
544
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
545
|
+
import {
|
|
546
|
+
CallToolRequestSchema,
|
|
547
|
+
ListToolsRequestSchema
|
|
548
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
549
|
+
async function createServer(options) {
|
|
550
|
+
const runner = new DrumcodeRunner(options);
|
|
551
|
+
const authContext = await runner.initializeAuth();
|
|
552
|
+
if (authContext.isAuthenticated) {
|
|
553
|
+
console.error(`[drumcode] Authenticated as: ${authContext.tokenName || "unnamed token"}`);
|
|
554
|
+
console.error(`[drumcode] Project ID: ${authContext.projectId}`);
|
|
555
|
+
if (authContext.profileId) {
|
|
556
|
+
console.error(`[drumcode] Permission profile: ${authContext.profileId}`);
|
|
557
|
+
}
|
|
558
|
+
} else if (options.token) {
|
|
559
|
+
console.error("[drumcode] Warning: Token provided but authentication failed");
|
|
560
|
+
}
|
|
561
|
+
const server = new Server(
|
|
562
|
+
{
|
|
563
|
+
name: "drumcode",
|
|
564
|
+
version: "0.1.0"
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
capabilities: {
|
|
568
|
+
tools: {}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
);
|
|
572
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
573
|
+
const capabilities = {
|
|
574
|
+
type: options.clientMode === "full" ? "anthropic" : "unknown",
|
|
575
|
+
supportsAdvancedToolUse: options.clientMode === "full",
|
|
576
|
+
supportsDeferredLoading: options.clientMode === "full"
|
|
577
|
+
};
|
|
578
|
+
const tools = await runner.getToolList(capabilities);
|
|
579
|
+
const demoTools = [
|
|
580
|
+
{
|
|
581
|
+
name: "drumcode_echo",
|
|
582
|
+
description: "Echo back the input arguments. Useful for testing.",
|
|
583
|
+
input_schema: {
|
|
584
|
+
type: "object",
|
|
585
|
+
properties: {
|
|
586
|
+
message: {
|
|
587
|
+
type: "string",
|
|
588
|
+
description: "The message to echo back"
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
required: ["message"]
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
name: "drumcode_http_get",
|
|
596
|
+
description: "Make an HTTP GET request to a URL and return the response.",
|
|
597
|
+
input_schema: {
|
|
598
|
+
type: "object",
|
|
599
|
+
properties: {
|
|
600
|
+
url: {
|
|
601
|
+
type: "string",
|
|
602
|
+
description: "The URL to fetch"
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
required: ["url"]
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
];
|
|
609
|
+
const arxivTools = [
|
|
610
|
+
{
|
|
611
|
+
name: "arxiv_search_papers",
|
|
612
|
+
description: "Search for academic papers on arXiv. Returns papers matching the query with optional filters.",
|
|
613
|
+
input_schema: {
|
|
614
|
+
type: "object",
|
|
615
|
+
properties: {
|
|
616
|
+
query: {
|
|
617
|
+
type: "string",
|
|
618
|
+
description: 'Search query keywords (e.g., "machine learning", "quantum computing")'
|
|
619
|
+
},
|
|
620
|
+
max_results: {
|
|
621
|
+
type: "number",
|
|
622
|
+
description: "Maximum number of results to return (1-20)"
|
|
623
|
+
},
|
|
624
|
+
categories: {
|
|
625
|
+
type: "string",
|
|
626
|
+
description: 'Comma-separated arXiv categories to filter by (e.g., "cs.AI,cs.LG")'
|
|
627
|
+
}
|
|
628
|
+
},
|
|
629
|
+
required: ["query"]
|
|
630
|
+
}
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
name: "arxiv_get_paper",
|
|
634
|
+
description: "Get detailed metadata and abstract for a specific paper by its arXiv ID.",
|
|
635
|
+
input_schema: {
|
|
636
|
+
type: "object",
|
|
637
|
+
properties: {
|
|
638
|
+
paper_id: {
|
|
639
|
+
type: "string",
|
|
640
|
+
description: 'The arXiv paper ID (e.g., "2301.07041" or "1706.03762")'
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
required: ["paper_id"]
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
{
|
|
647
|
+
name: "arxiv_search_by_author",
|
|
648
|
+
description: "Search for papers by a specific author on arXiv.",
|
|
649
|
+
input_schema: {
|
|
650
|
+
type: "object",
|
|
651
|
+
properties: {
|
|
652
|
+
author: {
|
|
653
|
+
type: "string",
|
|
654
|
+
description: "Author name to search for"
|
|
655
|
+
},
|
|
656
|
+
max_results: {
|
|
657
|
+
type: "number",
|
|
658
|
+
description: "Maximum number of results (1-20)"
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
required: ["author"]
|
|
662
|
+
}
|
|
663
|
+
},
|
|
664
|
+
{
|
|
665
|
+
name: "arxiv_search_by_category",
|
|
666
|
+
description: "Search for recent papers in a specific arXiv category.",
|
|
667
|
+
input_schema: {
|
|
668
|
+
type: "object",
|
|
669
|
+
properties: {
|
|
670
|
+
category: {
|
|
671
|
+
type: "string",
|
|
672
|
+
description: 'arXiv category (e.g., "cs.AI", "math.CO", "physics.hep-th")'
|
|
673
|
+
},
|
|
674
|
+
max_results: {
|
|
675
|
+
type: "number",
|
|
676
|
+
description: "Maximum number of results (1-20)"
|
|
677
|
+
}
|
|
678
|
+
},
|
|
679
|
+
required: ["category"]
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
];
|
|
683
|
+
const allTools = [...tools, ...demoTools, ...arxivTools].map((t) => ({
|
|
684
|
+
name: t.name,
|
|
685
|
+
description: t.description,
|
|
686
|
+
inputSchema: {
|
|
687
|
+
type: "object",
|
|
688
|
+
properties: t.input_schema.properties,
|
|
689
|
+
required: t.input_schema.required
|
|
690
|
+
}
|
|
691
|
+
}));
|
|
692
|
+
return { tools: allTools };
|
|
693
|
+
});
|
|
694
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
695
|
+
const { name, arguments: args } = request.params;
|
|
696
|
+
const result = await runner.executeTool({
|
|
697
|
+
name,
|
|
698
|
+
arguments: args ?? {}
|
|
699
|
+
});
|
|
700
|
+
return {
|
|
701
|
+
content: result.content,
|
|
702
|
+
isError: result.isError
|
|
703
|
+
};
|
|
704
|
+
});
|
|
705
|
+
const transport = new StdioServerTransport();
|
|
706
|
+
await server.connect(transport);
|
|
707
|
+
console.error("[drumcode] MCP Server started");
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
export {
|
|
711
|
+
DrumcodeRunner,
|
|
712
|
+
createServer
|
|
713
|
+
};
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|