Skip to content

Commit f9b703f

Browse files
chore: minor implmentation
1 parent 2eee80d commit f9b703f

File tree

9 files changed

+223
-28
lines changed

9 files changed

+223
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.1",
44
"scripts": {
55
"commit": "czg",
6-
"setup": "is-ci || husky",
6+
"prepare": "is-ci || husky",
77
"format": "biome check --write ."
88
},
99
"devDependencies": {

packages/core/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
},
2727
"peerDependencies": {
2828
"@modelcontextprotocol/sdk": "^1.7.0",
29-
"pino": "^9.6.0"
29+
"@standard-community/standard-json": "^0.1.0",
30+
"@valibot/to-json-schema": "^1.0.0-rc.0",
31+
"arktype": "^2.0.4",
32+
"pino": "^9.6.0",
33+
"zod-to-json-schema": "^3.24.1"
3034
},
3135
"devDependencies": {
3236
"@standard-schema/spec": "^1.0.0",

packages/core/src/handlers/initializeHandler.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
InitializeRequestSchema,
33
LATEST_PROTOCOL_VERSION,
44
SUPPORTED_PROTOCOL_VERSIONS,
5+
type ServerCapabilities,
56
} from "@modelcontextprotocol/sdk/types.js";
67
import type { HandlerFn } from "../types";
78

@@ -14,6 +15,12 @@ export const initializeHandler: HandlerFn = (ctx, request, config) => {
1415
const validatedData = InitializeRequestSchema.parse(request);
1516
const requestedVersion = validatedData.params.protocolVersion;
1617

18+
const capabilities: ServerCapabilities = {};
19+
20+
if (config.tools) {
21+
capabilities.tools = {};
22+
}
23+
1724
return {
1825
result: {
1926
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion)
@@ -23,9 +30,7 @@ export const initializeHandler: HandlerFn = (ctx, request, config) => {
2330
name: config.name,
2431
version: config.version,
2532
},
26-
capabilities: {
27-
tools: {},
28-
},
33+
capabilities,
2934
},
3035
};
3136
};

packages/core/src/handlers/messageHandler.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ import {
55
import type { HandlerFn } from "../types";
66
import { initializeHandler } from "./initializeHandler";
77
import { methodNotFoundHandler } from "./notFound";
8+
import { callToolsHandler, listToolsHandler } from "./tools";
89

910
const handlersMap: Record<string, HandlerFn | undefined> = {
1011
initialize: initializeHandler,
12+
"tools/call": callToolsHandler,
13+
"tools/list": listToolsHandler,
1114
};
1215

1316
export const messageHandler: HandlerFn = async (ctx, request, config) => {
1417
const _request = RequestSchema.parse(request);
1518

16-
const handler = handlersMap[_request.method] ?? methodNotFoundHandler;
19+
const method = _request.method;
20+
21+
if ((method === "tools/list" || method === "tools/call") && !config.tools) {
22+
return;
23+
}
24+
25+
const handler = handlersMap[method] ?? methodNotFoundHandler;
1726

1827
const response = await handler(ctx, request, config);
1928

2029
ctx.logger.info({
21-
msg: `Response for ${_request.method}`,
30+
msg: `Response for ${method}`,
2231
response,
2332
});
2433

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { HandlerFn } from "@/types";
2+
3+
export const callToolsHandler: HandlerFn = () => {};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./listToolsHandler";
2+
export * from "./callToolsHandler";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { HandlerFn } from "@/types";
2+
import { ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
3+
import { toJsonSchema } from "@standard-community/standard-json";
4+
5+
const EMPTY_OBJECT_JSON_SCHEMA = {
6+
type: "object" as const,
7+
};
8+
9+
export const listToolsHandler: HandlerFn = async (ctx, request, config) => {
10+
ctx.logger.info({
11+
msg: "Listing tools",
12+
request,
13+
});
14+
15+
if (!config.tools) {
16+
return;
17+
}
18+
19+
const validatedData = ListToolsRequestSchema.parse(request);
20+
21+
return {
22+
result: {
23+
tools: await Promise.all(
24+
Object.entries(config.tools).map(async ([name, tool]) => ({
25+
name,
26+
description: tool.description,
27+
inputSchema: tool.schema
28+
? await toJsonSchema(tool.schema)
29+
: EMPTY_OBJECT_JSON_SCHEMA,
30+
})),
31+
),
32+
},
33+
};
34+
};

packages/core/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
"forceConsistentCasingInFileNames": true,
2525
"isolatedModules": true,
2626
"verbatimModuleSyntax": true,
27-
"skipLibCheck": true
27+
"skipLibCheck": true,
28+
"baseUrl": ".",
29+
"paths": {
30+
"@/*": ["./src/*"]
31+
}
2832
},
2933
"include": ["./src/**/*"]
3034
}

0 commit comments

Comments
 (0)