unicommand 0.0.4 → 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 +46 -0
- package/dist/index.d.ts +66 -17
- package/dist/index.js +271 -39
- package/dist/index.js.map +1 -1
- package/package.json +7 -5
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { z as
|
|
2
|
+
import { z as z5 } from "zod";
|
|
3
3
|
|
|
4
4
|
// src/command/runtime-metadata.ts
|
|
5
5
|
var commandMetadata = /* @__PURE__ */ new WeakMap();
|
|
@@ -1087,14 +1087,16 @@ var createCommandCliRunner = ({
|
|
|
1087
1087
|
};
|
|
1088
1088
|
|
|
1089
1089
|
// src/mcp/router.ts
|
|
1090
|
-
import { z as
|
|
1090
|
+
import { z as z4 } from "zod";
|
|
1091
1091
|
|
|
1092
1092
|
// src/mcp/constants.ts
|
|
1093
1093
|
var DEFAULT_MCP_SERVER_VERSION = "0.0.0";
|
|
1094
1094
|
var DEFAULT_RESOURCE_MIME_TYPE = "application/json";
|
|
1095
|
-
var
|
|
1095
|
+
var MCP_SUPPORTED_PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
|
|
1096
1096
|
var JSON_RPC_METHOD_NOT_FOUND = -32601;
|
|
1097
1097
|
var JSON_RPC_INTERNAL_ERROR = -32603;
|
|
1098
|
+
var JSON_RPC_INVALID_PARAMS = -32602;
|
|
1099
|
+
var JSON_RPC_PARSE_ERROR = -32700;
|
|
1098
1100
|
var JSON_RPC_VERSION = "2.0";
|
|
1099
1101
|
var MCP_STATUS_COMPLETED = "completed";
|
|
1100
1102
|
var MCP_STATUS_ERROR = "error";
|
|
@@ -1107,6 +1109,7 @@ var RESULT_ENVELOPE_PROGRESS_KEY = "progress";
|
|
|
1107
1109
|
var PROGRESS_LIST_KEYS = ["items", "threads", "files", "events", "messages"];
|
|
1108
1110
|
var CURSOR_KEYS = ["nextCursor", "nextPageToken"];
|
|
1109
1111
|
var MCP_METHOD_INITIALIZE = "initialize";
|
|
1112
|
+
var MCP_METHOD_PING = "ping";
|
|
1110
1113
|
var MCP_METHOD_TOOLS_LIST = "tools/list";
|
|
1111
1114
|
var MCP_METHOD_TOOLS_CALL = "tools/call";
|
|
1112
1115
|
var MCP_METHOD_RESOURCES_LIST = "resources/list";
|
|
@@ -1114,16 +1117,27 @@ var MCP_METHOD_RESOURCES_TEMPLATES_LIST = "resources/templates/list";
|
|
|
1114
1117
|
var MCP_METHOD_RESOURCES_READ = "resources/read";
|
|
1115
1118
|
|
|
1116
1119
|
// src/mcp/errors.ts
|
|
1120
|
+
var McpProtocolError = class extends Error {
|
|
1121
|
+
constructor(code, message) {
|
|
1122
|
+
super(message);
|
|
1123
|
+
this.code = code;
|
|
1124
|
+
}
|
|
1125
|
+
code;
|
|
1126
|
+
};
|
|
1117
1127
|
var assertToolName = (expectedName, actualName) => {
|
|
1118
1128
|
if (actualName !== expectedName) {
|
|
1119
1129
|
throwUnknownTool(actualName);
|
|
1120
1130
|
}
|
|
1121
1131
|
};
|
|
1122
1132
|
var throwUnknownTool = (name) => {
|
|
1123
|
-
throw new
|
|
1133
|
+
throw new McpProtocolError(
|
|
1134
|
+
JSON_RPC_INVALID_PARAMS,
|
|
1135
|
+
`Unknown tool: ${name ?? EMPTY_DISPLAY_VALUE2}`
|
|
1136
|
+
);
|
|
1124
1137
|
};
|
|
1125
1138
|
|
|
1126
1139
|
// src/mcp/progress.ts
|
|
1140
|
+
import { z as z2 } from "zod";
|
|
1127
1141
|
var toolResult = (result, startedAt) => {
|
|
1128
1142
|
const envelope = resultEnvelope(result);
|
|
1129
1143
|
const data = envelope.data;
|
|
@@ -1137,16 +1151,22 @@ var toolResult = (result, startedAt) => {
|
|
|
1137
1151
|
content: [{ type: MCP_TEXT_CONTENT_TYPE, text: JSON.stringify(structuredContent, null, 2) }]
|
|
1138
1152
|
};
|
|
1139
1153
|
};
|
|
1154
|
+
var errorMessage = (caught) => {
|
|
1155
|
+
if (isZodError(caught)) return `Invalid input:
|
|
1156
|
+
${z2.prettifyError(caught)}`;
|
|
1157
|
+
return caught instanceof Error ? caught.message : String(caught);
|
|
1158
|
+
};
|
|
1159
|
+
var isZodError = (caught) => caught instanceof Error && caught.name === "ZodError" && Array.isArray(caught.issues);
|
|
1140
1160
|
var toolErrorResult = (caught, startedAt = Date.now()) => {
|
|
1141
|
-
const message =
|
|
1142
|
-
const
|
|
1161
|
+
const message = errorMessage(caught);
|
|
1162
|
+
const errorContent = {
|
|
1143
1163
|
status: MCP_STATUS_ERROR,
|
|
1144
1164
|
progress: { elapsedMs: elapsedMs(startedAt), errors: [message] },
|
|
1145
1165
|
error: { message }
|
|
1146
1166
|
};
|
|
1147
1167
|
return {
|
|
1148
|
-
|
|
1149
|
-
content: [{ type: MCP_TEXT_CONTENT_TYPE, text: JSON.stringify(
|
|
1168
|
+
isError: true,
|
|
1169
|
+
content: [{ type: MCP_TEXT_CONTENT_TYPE, text: JSON.stringify(errorContent, null, 2) }]
|
|
1150
1170
|
};
|
|
1151
1171
|
};
|
|
1152
1172
|
var commandProgressSummary = (data, command) => {
|
|
@@ -1253,11 +1273,11 @@ var resourceInputNames = (uriTemplate) => [...uriTemplate.matchAll(/\{([^}]+)\}/
|
|
|
1253
1273
|
});
|
|
1254
1274
|
|
|
1255
1275
|
// src/mcp/schema.ts
|
|
1256
|
-
import { z as
|
|
1276
|
+
import { z as z3 } from "zod";
|
|
1257
1277
|
var mcpToolOutputSchema = (dataSchema) => ({
|
|
1258
1278
|
type: "object",
|
|
1259
1279
|
properties: {
|
|
1260
|
-
data:
|
|
1280
|
+
data: z3.toJSONSchema(dataSchema),
|
|
1261
1281
|
progress: {
|
|
1262
1282
|
type: "object",
|
|
1263
1283
|
properties: {
|
|
@@ -1297,10 +1317,10 @@ var createCommandMcpRoute = ({
|
|
|
1297
1317
|
inputSchema,
|
|
1298
1318
|
outputSchema
|
|
1299
1319
|
});
|
|
1300
|
-
var commandMcpRouteToTool = (route, prefix) => ({
|
|
1320
|
+
var commandMcpRouteToTool = (route, prefix, boundOptions = {}) => ({
|
|
1301
1321
|
name: commandNameToMcpToolName(route.command.name, prefix),
|
|
1302
|
-
description: route.command
|
|
1303
|
-
inputSchema:
|
|
1322
|
+
description: commandMcpDescription(route.command),
|
|
1323
|
+
inputSchema: z4.toJSONSchema(commandMcpInputSchema(route, boundOptions)),
|
|
1304
1324
|
outputSchema: mcpToolOutputSchema(route.outputSchema),
|
|
1305
1325
|
annotations: commandMcpAnnotations(route.command)
|
|
1306
1326
|
});
|
|
@@ -1308,12 +1328,51 @@ var commandNameToMcpToolName = (name, prefix) => {
|
|
|
1308
1328
|
const baseName = name.replaceAll(COMMAND_NAME_SEPARATOR, TOOL_NAME_SEPARATOR);
|
|
1309
1329
|
return prefix ? `${prefix}_${baseName}` : baseName;
|
|
1310
1330
|
};
|
|
1311
|
-
var
|
|
1331
|
+
var commandMcpInputSchema = (route, boundOptions = {}) => {
|
|
1332
|
+
const schema = route.inputSchema;
|
|
1333
|
+
const shape = objectShape(schema);
|
|
1334
|
+
if (!shape) return schema;
|
|
1335
|
+
const hidden = hiddenInputNames(route.command, boundOptions);
|
|
1336
|
+
const required = new Set(
|
|
1337
|
+
(route.command.options ?? []).filter((option) => option.mcp === "required").map(optionInputName)
|
|
1338
|
+
);
|
|
1339
|
+
return z4.object(
|
|
1340
|
+
Object.fromEntries(
|
|
1341
|
+
Object.entries(shape).filter(([name]) => !hidden.has(name)).map(([name, property]) => [
|
|
1342
|
+
name,
|
|
1343
|
+
required.has(name) ? requiredProperty(property) : property
|
|
1344
|
+
])
|
|
1345
|
+
)
|
|
1346
|
+
);
|
|
1347
|
+
};
|
|
1348
|
+
var commandMcpInput = (route, args, boundOptions = {}) => {
|
|
1349
|
+
const inputNames = objectInputNames(route.inputSchema);
|
|
1350
|
+
const routeBoundOptions = Object.entries(boundOptions).filter(([name]) => inputNames.has(name));
|
|
1351
|
+
for (const [name, value] of routeBoundOptions) {
|
|
1352
|
+
if (args[name] !== void 0 && args[name] !== value) {
|
|
1353
|
+
throw new Error(`'${name}' is fixed to '${value}' by this MCP server`);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
commandMcpInputSchema(route, boundOptions).parse(args);
|
|
1357
|
+
const hidden = hiddenInputNames(route.command, boundOptions);
|
|
1358
|
+
const visibleArgs = Object.fromEntries(
|
|
1359
|
+
Object.entries(args).filter(([name]) => !hidden.has(name))
|
|
1360
|
+
);
|
|
1361
|
+
return route.inputSchema.parse({
|
|
1362
|
+
...visibleArgs,
|
|
1363
|
+
...Object.fromEntries(routeBoundOptions)
|
|
1364
|
+
});
|
|
1365
|
+
};
|
|
1366
|
+
var createCommandMcpRouter = ({
|
|
1367
|
+
boundOptions = {},
|
|
1368
|
+
prefix,
|
|
1369
|
+
routes
|
|
1370
|
+
}) => {
|
|
1312
1371
|
const mcpRoutes = routes.filter((route) => route.command.mcp);
|
|
1313
1372
|
const toolRoutes = mcpRoutes.filter((route) => isCommandMcpToolMetadata(route.command.mcp));
|
|
1314
1373
|
const resourceRoutes = mcpRoutes.flatMap(commandMcpRouteToResourceRoute);
|
|
1315
1374
|
return {
|
|
1316
|
-
tools: toolRoutes.map((route) => commandMcpRouteToTool(route, prefix)),
|
|
1375
|
+
tools: toolRoutes.map((route) => commandMcpRouteToTool(route, prefix, boundOptions)),
|
|
1317
1376
|
resources: resourceRoutes.filter((route) => !isResourceTemplate(route.uriTemplate)).map(commandResourceRouteToResource),
|
|
1318
1377
|
resourceTemplates: resourceRoutes.filter((route) => isResourceTemplate(route.uriTemplate)).map(commandResourceRouteToResourceTemplate),
|
|
1319
1378
|
callTool: async (params) => {
|
|
@@ -1324,7 +1383,7 @@ var createCommandMcpRouter = ({ prefix, routes }) => {
|
|
|
1324
1383
|
if (!route) {
|
|
1325
1384
|
return throwUnknownTool(toolCall.name);
|
|
1326
1385
|
}
|
|
1327
|
-
const input = route
|
|
1386
|
+
const input = commandMcpInput(route, toolCall.arguments ?? {}, boundOptions);
|
|
1328
1387
|
const data = route.outputSchema.parse(await route.handler(input));
|
|
1329
1388
|
return { data, progress: commandProgressSummary(data, route.command) };
|
|
1330
1389
|
},
|
|
@@ -1335,11 +1394,29 @@ var createCommandMcpRouter = ({ prefix, routes }) => {
|
|
|
1335
1394
|
throw new Error(`Unknown resource: ${resourceRead.uri ?? EMPTY_DISPLAY_VALUE2}`);
|
|
1336
1395
|
}
|
|
1337
1396
|
return routeMatch.route.command.outputSchema.parse(
|
|
1338
|
-
await routeMatch.route.handler(routeMatch.input)
|
|
1397
|
+
await routeMatch.route.handler({ ...routeMatch.input, ...boundOptions })
|
|
1339
1398
|
);
|
|
1340
1399
|
}
|
|
1341
1400
|
};
|
|
1342
1401
|
};
|
|
1402
|
+
var hiddenInputNames = (command, boundOptions) => /* @__PURE__ */ new Set([
|
|
1403
|
+
...Object.keys(boundOptions),
|
|
1404
|
+
...(command.options ?? []).filter((option) => option.mcp === false).map(optionInputName)
|
|
1405
|
+
]);
|
|
1406
|
+
var objectShape = (schema) => schema.def.type === "object" ? schema.shape : void 0;
|
|
1407
|
+
var requiredProperty = (property) => property.def.type === "optional" ? property.unwrap() : property;
|
|
1408
|
+
var objectInputNames = (schema) => new Set(Object.keys(objectShape(schema) ?? {}));
|
|
1409
|
+
var commandMcpDescription = (command) => {
|
|
1410
|
+
const mcp = command.mcp;
|
|
1411
|
+
if (!isCommandMcpToolMetadata(mcp)) return command.description;
|
|
1412
|
+
const notes = [
|
|
1413
|
+
...mcp.sideEffects?.length ? [`Side effects: ${mcp.sideEffects.join(" ")}`] : [],
|
|
1414
|
+
...mcp.requiresConfirmation ? ["Confirm with the user before calling this tool."] : []
|
|
1415
|
+
];
|
|
1416
|
+
return notes.length ? `${command.description}
|
|
1417
|
+
|
|
1418
|
+
${notes.join("\n")}` : command.description;
|
|
1419
|
+
};
|
|
1343
1420
|
var commandMcpAnnotations = (command) => {
|
|
1344
1421
|
const mcp = command.mcp;
|
|
1345
1422
|
if (!isCommandMcpToolMetadata(mcp)) return void 0;
|
|
@@ -1347,9 +1424,7 @@ var commandMcpAnnotations = (command) => {
|
|
|
1347
1424
|
readOnlyHint: mcp.readOnly,
|
|
1348
1425
|
destructiveHint: mcp.destructive,
|
|
1349
1426
|
idempotentHint: mcp.idempotent,
|
|
1350
|
-
openWorldHint: mcp.openWorld
|
|
1351
|
-
requiresConfirmation: mcp.requiresConfirmation,
|
|
1352
|
-
sideEffects: mcp.sideEffects
|
|
1427
|
+
openWorldHint: mcp.openWorld
|
|
1353
1428
|
};
|
|
1354
1429
|
};
|
|
1355
1430
|
|
|
@@ -1485,8 +1560,14 @@ var createSingleToolMcpServer = ({
|
|
|
1485
1560
|
}
|
|
1486
1561
|
});
|
|
1487
1562
|
};
|
|
1563
|
+
var negotiateMcpProtocolVersion = (params) => {
|
|
1564
|
+
const requested = params?.protocolVersion;
|
|
1565
|
+
return typeof requested === "string" && MCP_SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : MCP_SUPPORTED_PROTOCOL_VERSIONS[0];
|
|
1566
|
+
};
|
|
1488
1567
|
var createMcpServer = ({
|
|
1489
1568
|
name,
|
|
1569
|
+
version = DEFAULT_MCP_SERVER_VERSION,
|
|
1570
|
+
instructions,
|
|
1490
1571
|
tools,
|
|
1491
1572
|
resources = [],
|
|
1492
1573
|
resourceTemplates = [],
|
|
@@ -1503,15 +1584,24 @@ var createMcpServer = ({
|
|
|
1503
1584
|
const error = (id, code, message) => {
|
|
1504
1585
|
send({ jsonrpc: JSON_RPC_VERSION, id, error: { code, message } });
|
|
1505
1586
|
};
|
|
1587
|
+
const protocolError = (id, caught) => {
|
|
1588
|
+
const code = caught instanceof McpProtocolError ? caught.code : JSON_RPC_INTERNAL_ERROR;
|
|
1589
|
+
error(id, code, caught instanceof Error ? caught.message : String(caught));
|
|
1590
|
+
};
|
|
1506
1591
|
const handleRequest = async (request) => {
|
|
1507
1592
|
if (request.method === MCP_METHOD_INITIALIZE) {
|
|
1508
1593
|
result(request.id, {
|
|
1509
|
-
protocolVersion:
|
|
1594
|
+
protocolVersion: negotiateMcpProtocolVersion(request.params),
|
|
1510
1595
|
capabilities: { resources: {}, tools: {} },
|
|
1511
|
-
serverInfo: { name, version
|
|
1596
|
+
serverInfo: { name, version },
|
|
1597
|
+
instructions
|
|
1512
1598
|
});
|
|
1513
1599
|
return;
|
|
1514
1600
|
}
|
|
1601
|
+
if (request.method === MCP_METHOD_PING) {
|
|
1602
|
+
result(request.id, {});
|
|
1603
|
+
return;
|
|
1604
|
+
}
|
|
1515
1605
|
if (request.method === MCP_METHOD_TOOLS_LIST) {
|
|
1516
1606
|
result(request.id, { tools });
|
|
1517
1607
|
return;
|
|
@@ -1533,11 +1623,10 @@ var createMcpServer = ({
|
|
|
1533
1623
|
);
|
|
1534
1624
|
return;
|
|
1535
1625
|
}
|
|
1536
|
-
const startedAt = Date.now();
|
|
1537
1626
|
try {
|
|
1538
1627
|
result(request.id, resourceResult(request.params, await readResource(request.params)));
|
|
1539
1628
|
} catch (caught) {
|
|
1540
|
-
|
|
1629
|
+
protocolError(request.id, caught);
|
|
1541
1630
|
}
|
|
1542
1631
|
return;
|
|
1543
1632
|
}
|
|
@@ -1546,7 +1635,11 @@ var createMcpServer = ({
|
|
|
1546
1635
|
try {
|
|
1547
1636
|
result(request.id, toolResult(await callTool(request.params), startedAt));
|
|
1548
1637
|
} catch (caught) {
|
|
1549
|
-
|
|
1638
|
+
if (caught instanceof McpProtocolError) {
|
|
1639
|
+
protocolError(request.id, caught);
|
|
1640
|
+
} else {
|
|
1641
|
+
result(request.id, toolErrorResult(caught, startedAt));
|
|
1642
|
+
}
|
|
1550
1643
|
}
|
|
1551
1644
|
return;
|
|
1552
1645
|
}
|
|
@@ -1559,24 +1652,88 @@ var createMcpServer = ({
|
|
|
1559
1652
|
}
|
|
1560
1653
|
};
|
|
1561
1654
|
return {
|
|
1655
|
+
handleRequest,
|
|
1562
1656
|
start: async () => {
|
|
1563
1657
|
const input = createInterface({ input: process.stdin });
|
|
1658
|
+
const pending = /* @__PURE__ */ new Set();
|
|
1564
1659
|
for await (const line of input) {
|
|
1565
1660
|
if (!line.trim()) continue;
|
|
1661
|
+
let request;
|
|
1566
1662
|
try {
|
|
1567
|
-
|
|
1663
|
+
request = JSON.parse(line);
|
|
1568
1664
|
} catch (caught) {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
caught instanceof Error ? caught.message : String(caught)
|
|
1573
|
-
);
|
|
1665
|
+
const message = caught instanceof Error ? caught.message : String(caught);
|
|
1666
|
+
protocolError(void 0, new McpProtocolError(JSON_RPC_PARSE_ERROR, message));
|
|
1667
|
+
continue;
|
|
1574
1668
|
}
|
|
1669
|
+
const task = handleRequest(request).catch((caught) => protocolError(request.id, caught)).finally(() => pending.delete(task));
|
|
1670
|
+
pending.add(task);
|
|
1575
1671
|
}
|
|
1672
|
+
await Promise.all(pending);
|
|
1576
1673
|
}
|
|
1577
1674
|
};
|
|
1578
1675
|
};
|
|
1579
1676
|
|
|
1677
|
+
// src/mcp/server-options.ts
|
|
1678
|
+
var parseMcpServerArgv = (argv) => {
|
|
1679
|
+
const flags = {};
|
|
1680
|
+
const positionals = [];
|
|
1681
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
1682
|
+
const token = argv[index] ?? "";
|
|
1683
|
+
if (!token.startsWith("--")) {
|
|
1684
|
+
positionals.push(token);
|
|
1685
|
+
continue;
|
|
1686
|
+
}
|
|
1687
|
+
const [rawName, inlineValue] = splitFlag(token.slice(2));
|
|
1688
|
+
const nextToken = argv[index + 1];
|
|
1689
|
+
if (inlineValue !== void 0) {
|
|
1690
|
+
flags[kebabToCamel2(rawName)] = inlineValue;
|
|
1691
|
+
} else if (nextToken !== void 0 && !nextToken.startsWith("--")) {
|
|
1692
|
+
flags[kebabToCamel2(rawName)] = nextToken;
|
|
1693
|
+
index += 1;
|
|
1694
|
+
} else {
|
|
1695
|
+
flags[kebabToCamel2(rawName)] = "true";
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
return { flags, positionals };
|
|
1699
|
+
};
|
|
1700
|
+
var mcpServerOptionEnvName = (envPrefix, option) => option.env ?? `${envPrefix}_${camelToConstant(kebabToCamel2(option.name))}`;
|
|
1701
|
+
var resolveMcpServerOptions = ({
|
|
1702
|
+
argvFlags,
|
|
1703
|
+
definitions,
|
|
1704
|
+
env,
|
|
1705
|
+
envPrefix
|
|
1706
|
+
}) => {
|
|
1707
|
+
const values = {};
|
|
1708
|
+
const missing = [];
|
|
1709
|
+
for (const definition of definitions) {
|
|
1710
|
+
const inputName = kebabToCamel2(definition.name);
|
|
1711
|
+
const envName = mcpServerOptionEnvName(envPrefix, definition);
|
|
1712
|
+
const value = argvFlags[inputName] ?? nonEmpty(env[envName]);
|
|
1713
|
+
if (value !== void 0) {
|
|
1714
|
+
values[inputName] = value;
|
|
1715
|
+
} else if (definition.required) {
|
|
1716
|
+
missing.push(`--${definition.name} <value> (or ${envName})`);
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
if (missing.length > 0) {
|
|
1720
|
+
throw new Error(`Missing required MCP server option: ${missing.join(", ")}`);
|
|
1721
|
+
}
|
|
1722
|
+
return values;
|
|
1723
|
+
};
|
|
1724
|
+
var withoutMcpServerOptionFlags = (flags, definitions) => {
|
|
1725
|
+
const optionNames = new Set(definitions.map((definition) => kebabToCamel2(definition.name)));
|
|
1726
|
+
return Object.fromEntries(Object.entries(flags).filter(([name]) => !optionNames.has(name)));
|
|
1727
|
+
};
|
|
1728
|
+
var splitFlag = (flag) => {
|
|
1729
|
+
const separatorIndex = flag.indexOf("=");
|
|
1730
|
+
if (separatorIndex === -1) return [flag, void 0];
|
|
1731
|
+
return [flag.slice(0, separatorIndex), flag.slice(separatorIndex + 1)];
|
|
1732
|
+
};
|
|
1733
|
+
var nonEmpty = (value) => value?.trim() ? value : void 0;
|
|
1734
|
+
var kebabToCamel2 = (value) => value.replace(/-([a-z])/g, (_match, letter) => letter.toUpperCase());
|
|
1735
|
+
var camelToConstant = (value) => value.replace(/([A-Z])/g, "_$1").toUpperCase();
|
|
1736
|
+
|
|
1580
1737
|
// src/mcp/command-server.ts
|
|
1581
1738
|
var loadCommandMcpRoutes = async (commandsDir, getContext = () => void 0, importCommandModule2) => {
|
|
1582
1739
|
const modules = await loadCommandModules(commandsDir, importCommandModule2);
|
|
@@ -1587,38 +1744,103 @@ var loadCommandMcpRoutes = async (commandsDir, getContext = () => void 0, import
|
|
|
1587
1744
|
return factories.map((factory) => factory(getContext)).filter((route) => route.command.mcp);
|
|
1588
1745
|
};
|
|
1589
1746
|
var createCommandMcpServer = async ({
|
|
1747
|
+
argv = process.argv.slice(2),
|
|
1590
1748
|
commandsDir,
|
|
1749
|
+
env = process.env,
|
|
1750
|
+
envPrefix,
|
|
1591
1751
|
getContext,
|
|
1592
1752
|
importCommandModule: importCommandModule2,
|
|
1593
1753
|
importMetaUrl,
|
|
1754
|
+
instructions,
|
|
1594
1755
|
name,
|
|
1595
|
-
prefix
|
|
1756
|
+
prefix,
|
|
1757
|
+
serverOptions = [],
|
|
1758
|
+
version
|
|
1596
1759
|
}) => {
|
|
1597
1760
|
const commandDirectory = commandsDir ?? join5(dirname3(fileURLToPath4(importMetaUrl)), DEFAULT_COMMANDS_DIR);
|
|
1598
|
-
const
|
|
1599
|
-
|
|
1600
|
-
|
|
1761
|
+
const parsedArgv = parseMcpServerArgv(argv);
|
|
1762
|
+
const boundOptions = resolveMcpServerOptions({
|
|
1763
|
+
argvFlags: parsedArgv.flags,
|
|
1764
|
+
definitions: serverOptions,
|
|
1765
|
+
env,
|
|
1766
|
+
envPrefix: envPrefix ?? packageEnvPrefix(name)
|
|
1767
|
+
});
|
|
1768
|
+
const routes = await loadCommandMcpRoutes(
|
|
1769
|
+
commandDirectory,
|
|
1770
|
+
() => getContext ? getContext(boundOptions) : void 0,
|
|
1771
|
+
importCommandModule2
|
|
1772
|
+
);
|
|
1773
|
+
const router = createCommandMcpRouter({ boundOptions, prefix, routes });
|
|
1774
|
+
const server = createMcpServer({
|
|
1775
|
+
name,
|
|
1776
|
+
version,
|
|
1777
|
+
instructions: typeof instructions === "function" ? instructions(boundOptions) : instructions,
|
|
1778
|
+
tools: router.tools,
|
|
1779
|
+
resources: router.resources,
|
|
1780
|
+
resourceTemplates: router.resourceTemplates,
|
|
1781
|
+
callTool: router.callTool,
|
|
1782
|
+
readResource: router.readResource
|
|
1601
1783
|
});
|
|
1602
|
-
return
|
|
1784
|
+
return {
|
|
1785
|
+
...server,
|
|
1786
|
+
boundOptions,
|
|
1787
|
+
command: parsedArgv.positionals.length > 0 ? parsedArgv.positionals.join(" ") : void 0,
|
|
1788
|
+
runCommand: (commandName, flags = {}) => runMcpCommandOnce(routes, commandName, withoutMcpServerOptionFlags(flags, serverOptions), {
|
|
1789
|
+
boundOptions
|
|
1790
|
+
}),
|
|
1791
|
+
commandFlags: withoutMcpServerOptionFlags(parsedArgv.flags, serverOptions)
|
|
1792
|
+
};
|
|
1793
|
+
};
|
|
1794
|
+
var runMcpCommandOnce = async (routes, commandName, flags, { boundOptions }) => {
|
|
1795
|
+
const route = routes.find(
|
|
1796
|
+
(candidate) => candidate.command.name === commandName && isCommandMcpToolMetadata(candidate.command.mcp)
|
|
1797
|
+
);
|
|
1798
|
+
if (!route) throw new Error(`Unknown command: ${commandName}`);
|
|
1799
|
+
const input = commandMcpInput(route, oneShotInput(flags), boundOptions);
|
|
1800
|
+
return route.outputSchema.parse(await route.handler(input));
|
|
1603
1801
|
};
|
|
1604
1802
|
var startCommandMcpServer = async (options) => {
|
|
1605
|
-
|
|
1803
|
+
try {
|
|
1804
|
+
const server = await createCommandMcpServer(options);
|
|
1805
|
+
if (!server.command) {
|
|
1806
|
+
await server.start();
|
|
1807
|
+
return;
|
|
1808
|
+
}
|
|
1809
|
+
const data = await server.runCommand(server.command, server.commandFlags);
|
|
1810
|
+
process.stdout.write(`${JSON.stringify(data)}
|
|
1811
|
+
`);
|
|
1812
|
+
} catch (caught) {
|
|
1813
|
+
process.stderr.write(`${errorMessage(caught)}
|
|
1814
|
+
`);
|
|
1815
|
+
process.exitCode = 1;
|
|
1816
|
+
}
|
|
1606
1817
|
};
|
|
1607
1818
|
var startPackageCommandMcpServer = async ({
|
|
1819
|
+
envPrefix,
|
|
1608
1820
|
importMetaUrl,
|
|
1609
1821
|
mcpServerPrefix,
|
|
1610
1822
|
name,
|
|
1611
1823
|
prefix,
|
|
1824
|
+
version,
|
|
1612
1825
|
...options
|
|
1613
1826
|
}) => {
|
|
1614
1827
|
const metadata = packageCommandMetadata(importMetaUrl, { mcpServerPrefix });
|
|
1615
1828
|
await startCommandMcpServer({
|
|
1616
1829
|
...options,
|
|
1830
|
+
envPrefix: envPrefix ?? packageEnvPrefix(metadata.packageName),
|
|
1617
1831
|
importMetaUrl,
|
|
1618
1832
|
name: name ?? metadata.mcpServerName,
|
|
1619
|
-
prefix: prefix ?? metadata.mcpPrefix
|
|
1833
|
+
prefix: prefix ?? metadata.mcpPrefix,
|
|
1834
|
+
version: version ?? metadata.version
|
|
1620
1835
|
});
|
|
1621
1836
|
};
|
|
1837
|
+
var ONE_SHOT_BOOLEANS = /* @__PURE__ */ new Map([
|
|
1838
|
+
["true", true],
|
|
1839
|
+
["false", false]
|
|
1840
|
+
]);
|
|
1841
|
+
var oneShotInput = (flags) => Object.fromEntries(
|
|
1842
|
+
Object.entries(flags).map(([name, value]) => [name, ONE_SHOT_BOOLEANS.get(value) ?? value])
|
|
1843
|
+
);
|
|
1622
1844
|
|
|
1623
1845
|
// src/scripts/dev-command.ts
|
|
1624
1846
|
import { mkdirSync, rmSync as rmSync2 } from "fs";
|
|
@@ -1742,6 +1964,7 @@ var packageDevCommandConfig = (importMetaUrl, options) => {
|
|
|
1742
1964
|
};
|
|
1743
1965
|
export {
|
|
1744
1966
|
DEFAULT_COMMANDS_DIR,
|
|
1967
|
+
McpProtocolError,
|
|
1745
1968
|
PROGRAM_NAME_ENV_SUFFIX,
|
|
1746
1969
|
PackageManager,
|
|
1747
1970
|
assertToolName,
|
|
@@ -1752,6 +1975,8 @@ export {
|
|
|
1752
1975
|
commandExportsFromModules,
|
|
1753
1976
|
commandInputString,
|
|
1754
1977
|
commandInputValue,
|
|
1978
|
+
commandMcpInput,
|
|
1979
|
+
commandMcpInputSchema,
|
|
1755
1980
|
commandMcpRouteToResourceRoute,
|
|
1756
1981
|
commandMcpRouteToTool,
|
|
1757
1982
|
commandNameToMcpToolName,
|
|
@@ -1790,12 +2015,16 @@ export {
|
|
|
1790
2015
|
loadCommandMcpRoutes,
|
|
1791
2016
|
loadCommandModules,
|
|
1792
2017
|
loadCommandModulesFromFiles,
|
|
2018
|
+
mcpServerOptionEnvName,
|
|
1793
2019
|
mcpToolOutputSchema,
|
|
2020
|
+
negotiateMcpProtocolVersion,
|
|
1794
2021
|
normalizeCommandInputAliases,
|
|
2022
|
+
optionInputName,
|
|
1795
2023
|
packageBinEntry,
|
|
1796
2024
|
packageCommandMetadata,
|
|
1797
2025
|
packageEnvPrefix,
|
|
1798
2026
|
packageRoot,
|
|
2027
|
+
parseMcpServerArgv,
|
|
1799
2028
|
printProgramHelp,
|
|
1800
2029
|
readPackageJson,
|
|
1801
2030
|
registerCliCommandsFromDirectory,
|
|
@@ -1803,7 +2032,9 @@ export {
|
|
|
1803
2032
|
registerCompletionCommand,
|
|
1804
2033
|
registerParentCommandsFromDefinitions,
|
|
1805
2034
|
registerUpdateCommand,
|
|
2035
|
+
resolveMcpServerOptions,
|
|
1806
2036
|
resourceResult,
|
|
2037
|
+
runMcpCommandOnce,
|
|
1807
2038
|
setProgramHelpData,
|
|
1808
2039
|
shortPackageName,
|
|
1809
2040
|
startCommandMcpServer,
|
|
@@ -1812,6 +2043,7 @@ export {
|
|
|
1812
2043
|
uninstallDevCommand,
|
|
1813
2044
|
uninstallPackageDevCommand,
|
|
1814
2045
|
uninstallPackageDevCommands,
|
|
1815
|
-
|
|
2046
|
+
withoutMcpServerOptionFlags,
|
|
2047
|
+
z5 as z
|
|
1816
2048
|
};
|
|
1817
2049
|
//# sourceMappingURL=index.js.map
|