@donghanh/hono 0.3.4 → 0.3.5
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/package.json +1 -1
- package/src/mcp.test.ts +34 -0
- package/src/mcp.ts +30 -1
package/package.json
CHANGED
package/src/mcp.test.ts
CHANGED
|
@@ -259,6 +259,40 @@ describe("mcpRoutes", () => {
|
|
|
259
259
|
expect(result.structuredContent.display).toEqual({ x: 1 });
|
|
260
260
|
});
|
|
261
261
|
|
|
262
|
+
test("tools/call: enrichResponse can add fields to structuredContent", async () => {
|
|
263
|
+
const app = mcpRoutes({
|
|
264
|
+
...baseConfig,
|
|
265
|
+
registry: makeApp({ q: makeOp("q") }),
|
|
266
|
+
executor: async () => ({ v: 1 }),
|
|
267
|
+
enrichResponse: async (structured, opts) => {
|
|
268
|
+
structured.display = `${opts.operationId}:${opts.userId}`;
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
const { result } = await rpc(app, "tools/call", { name: "q" });
|
|
272
|
+
expect(result.structuredContent.display).toBe("q:u1");
|
|
273
|
+
expect(result.structuredContent.q).toEqual({ v: 1 });
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("tools/call: enrichResponse receives the call arguments", async () => {
|
|
277
|
+
let seen: Record<string, unknown> | undefined;
|
|
278
|
+
const app = mcpRoutes({
|
|
279
|
+
...baseConfig,
|
|
280
|
+
registry: makeApp({ q: makeOp("q") }),
|
|
281
|
+
enrichResponse: async (_structured, opts) => {
|
|
282
|
+
seen = opts.variables;
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
await rpc(app, "tools/call", { name: "q", arguments: { groupId: "g1" } });
|
|
286
|
+
expect(seen?.groupId).toBe("g1");
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test("GET /mcp → 405 with Allow: POST", async () => {
|
|
290
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
291
|
+
const res = await app.request("/mcp");
|
|
292
|
+
expect(res.status).toBe(405);
|
|
293
|
+
expect(res.headers.get("allow")).toBe("POST");
|
|
294
|
+
});
|
|
295
|
+
|
|
262
296
|
test("unknown tool → error", async () => {
|
|
263
297
|
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
264
298
|
const { error } = await rpc(app, "tools/call", { name: "nope" });
|
package/src/mcp.ts
CHANGED
|
@@ -37,6 +37,17 @@ export interface McpRoutesConfig {
|
|
|
37
37
|
encodeVariables?: (vars: Record<string, unknown>) => string;
|
|
38
38
|
/** Named widgets. Operation's `widget` config key must match a name here. */
|
|
39
39
|
widgets?: Record<string, WidgetConfig>;
|
|
40
|
+
/** Hook to enrich `structuredContent` after execution (e.g. inject display text) */
|
|
41
|
+
enrichResponse?: (
|
|
42
|
+
structured: Record<string, unknown>,
|
|
43
|
+
opts: {
|
|
44
|
+
operationId: string;
|
|
45
|
+
data: unknown;
|
|
46
|
+
variables: Record<string, unknown>;
|
|
47
|
+
userId: string | null;
|
|
48
|
+
request: Request;
|
|
49
|
+
},
|
|
50
|
+
) => Promise<void>;
|
|
40
51
|
}
|
|
41
52
|
|
|
42
53
|
type JsonRpcRequest = {
|
|
@@ -136,6 +147,7 @@ export function mcpRoutes(config: McpRoutesConfig): Hono {
|
|
|
136
147
|
serverInfo = { name: "donghanh-mcp", version: "0.1.0" },
|
|
137
148
|
encodeVariables,
|
|
138
149
|
widgets = {},
|
|
150
|
+
enrichResponse,
|
|
139
151
|
} = config;
|
|
140
152
|
|
|
141
153
|
const app = new Hono();
|
|
@@ -152,6 +164,12 @@ export function mcpRoutes(config: McpRoutesConfig): Hono {
|
|
|
152
164
|
}),
|
|
153
165
|
);
|
|
154
166
|
|
|
167
|
+
// Stateless server: no SSE stream to open
|
|
168
|
+
app.get(
|
|
169
|
+
"/mcp",
|
|
170
|
+
() => new Response(null, { status: 405, headers: { Allow: "POST" } }),
|
|
171
|
+
);
|
|
172
|
+
|
|
155
173
|
app.post("/mcp", async (c) => {
|
|
156
174
|
let body: JsonRpcRequest;
|
|
157
175
|
try {
|
|
@@ -290,11 +308,12 @@ export function mcpRoutes(config: McpRoutesConfig): Hono {
|
|
|
290
308
|
}
|
|
291
309
|
}
|
|
292
310
|
|
|
311
|
+
const variables = params.arguments ?? {};
|
|
293
312
|
try {
|
|
294
313
|
const { data, brief } = await executeOperation({
|
|
295
314
|
registry,
|
|
296
315
|
operationId: toolName,
|
|
297
|
-
variables
|
|
316
|
+
variables,
|
|
298
317
|
executor,
|
|
299
318
|
context: { userId, request: c.req.raw },
|
|
300
319
|
});
|
|
@@ -314,6 +333,16 @@ export function mcpRoutes(config: McpRoutesConfig): Hono {
|
|
|
314
333
|
structured.display = rendered.display;
|
|
315
334
|
}
|
|
316
335
|
|
|
336
|
+
if (enrichResponse) {
|
|
337
|
+
await enrichResponse(structured, {
|
|
338
|
+
operationId: toolName,
|
|
339
|
+
data,
|
|
340
|
+
variables,
|
|
341
|
+
userId,
|
|
342
|
+
request: c.req.raw,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
317
346
|
const meta: Record<string, unknown> = {};
|
|
318
347
|
if (rendered.context && rendered.context.length > 0) {
|
|
319
348
|
meta["donghanh/context"] = rendered.context;
|