feat(mcp): add typed bounded analysis tools

This commit is contained in:
morluto
2026-07-12 18:59:53 +08:00
parent 7e4ae21d78
commit 893d2e2d05
16 changed files with 509 additions and 100 deletions
+7 -1
View File
@@ -32,12 +32,18 @@ const registerEnhancedTool = (
const name = enhancedToolNameSchema.parse(contract.name);
server.registerTool(
name,
{ description: contract.description, inputSchema: contract.inputSchema },
{
description: contract.description,
inputSchema: contract.inputSchema,
outputSchema: contract.outputSchema,
annotations: contract.annotations,
},
async (input, context) =>
toCallToolResult(
await logToolExecution(logger, name, () =>
services.execute(name, input, context.mcpReq.signal),
),
contract,
),
);
};
+3 -1
View File
@@ -32,6 +32,8 @@ const registerOfficialTool = (
{
description: contract.description,
inputSchema: contract.inputSchema,
outputSchema: contract.outputSchema,
annotations: contract.annotations,
},
async (input, context) => {
const arguments_ = projectOfficialArguments(contract, input);
@@ -40,7 +42,7 @@ const registerOfficialTool = (
signal: context.mcpReq.signal,
}),
);
return toCallToolResult(result);
return toCallToolResult(result, contract);
},
);
};
+21 -10
View File
@@ -19,6 +19,8 @@ export const registerSessionTools = (
{
description: openContract.description,
inputSchema: openContract.inputSchema,
outputSchema: openContract.outputSchema,
annotations: openContract.annotations,
},
async (input, context) => {
const parsed = z.object({ path: z.string().min(1) }).parse(input);
@@ -26,16 +28,19 @@ export const registerSessionTools = (
session.open(parsed.path, { signal: context.mcpReq.signal }),
);
return opened.ok
? toCallToolResult({
ok: true,
value: {
path: opened.value.path,
format: opened.value.format,
kind: opened.value.kind,
loaderArgs: [...opened.value.loaderArgs],
? toCallToolResult(
{
ok: true,
value: {
path: opened.value.path,
format: opened.value.format,
kind: opened.value.kind,
loaderArgs: [...opened.value.loaderArgs],
},
},
})
: toCallToolResult(opened);
openContract,
)
: toCallToolResult(opened, openContract);
},
);
server.registerTool(
@@ -43,12 +48,15 @@ export const registerSessionTools = (
{
description: closeContract.description,
inputSchema: closeContract.inputSchema,
outputSchema: closeContract.outputSchema,
annotations: closeContract.annotations,
},
async () =>
toCallToolResult(
await logToolExecution(logger, closeContract.name, () =>
session.close(),
),
closeContract,
),
);
server.registerTool(
@@ -56,7 +64,10 @@ export const registerSessionTools = (
{
description: statusContract.description,
inputSchema: statusContract.inputSchema,
outputSchema: statusContract.outputSchema,
annotations: statusContract.annotations,
},
() => toCallToolResult({ ok: true, value: session.status() }),
() =>
toCallToolResult({ ok: true, value: session.status() }, statusContract),
);
};
+30 -1
View File
@@ -3,6 +3,7 @@ import type { CallToolResult } from "@modelcontextprotocol/server";
import type { HopperError } from "../domain/errors.js";
import type { Result } from "../domain/result.js";
import type { JsonValue } from "../hopper/protocol.js";
import type { ToolContract } from "../contracts/toolContracts.js";
/**
* Translate an application result into MCP text content.
@@ -11,9 +12,10 @@ import type { JsonValue } from "../hopper/protocol.js";
*/
export const toCallToolResult = (
result: Result<JsonValue, HopperError>,
contract: ToolContract,
): CallToolResult =>
result.ok
? { content: [{ type: "text", text: formatValue(result.value) }] }
? successResult(result.value, contract)
: {
content: [
{
@@ -24,6 +26,33 @@ export const toCallToolResult = (
isError: true,
};
const successResult = (
value: JsonValue,
contract: ToolContract,
): CallToolResult => {
const candidate = ["list_procedures", "list_names", "list_strings"].includes(
contract.name,
)
? value
: { result: value };
const parsed = contract.outputSchema.safeParse(candidate);
if (!parsed.success) {
return {
content: [
{
type: "text",
text: "HopperProtocolError: Hopper returned output that does not match the tool contract",
},
],
isError: true,
};
}
return {
content: [{ type: "text", text: formatValue(value) }],
structuredContent: candidate,
};
};
const formatValue = (value: JsonValue): string => {
if (value === null) return "OK";
if (typeof value === "string") return value;