Skip to content

Commit 916a361

Browse files
committed
clean version tool
1 parent 1d07b56 commit 916a361

File tree

3 files changed

+69
-69
lines changed

3 files changed

+69
-69
lines changed

src/tools/BaseTool.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type {
2+
McpServer,
3+
RegisteredTool
4+
} from '@modelcontextprotocol/sdk/server/mcp.js';
5+
import type {
6+
ToolAnnotations,
7+
CallToolResult
8+
} from '@modelcontextprotocol/sdk/types.js';
9+
import type { ZodTypeAny } from 'zod';
10+
import type { z } from 'zod';
11+
12+
export abstract class BaseTool<InputSchema extends ZodTypeAny> {
13+
abstract readonly name: string;
14+
abstract readonly description: string;
15+
abstract readonly annotations: ToolAnnotations;
16+
17+
readonly inputSchema: InputSchema;
18+
protected server: McpServer | null = null;
19+
20+
constructor(params: { inputSchema: InputSchema }) {
21+
this.inputSchema = params.inputSchema;
22+
}
23+
24+
/**
25+
* Installs the tool to the given MCP server.
26+
*/
27+
installTo(server: McpServer): RegisteredTool {
28+
this.server = server;
29+
return server.registerTool(
30+
this.name,
31+
{
32+
title: this.annotations.title,
33+
description: this.description,
34+
inputSchema: (this.inputSchema as unknown as z.ZodObject<any>).shape,
35+
annotations: this.annotations
36+
},
37+
(args, extra) => this.run(args, extra)
38+
);
39+
}
40+
41+
/**
42+
* Tool logic to be implemented by subclasses.
43+
*/
44+
abstract run(rawInput: unknown, extra?: any): Promise<CallToolResult>;
45+
46+
/**
47+
* Helper method to send logging messages
48+
*/
49+
protected log(
50+
level: 'debug' | 'info' | 'warning' | 'error',
51+
data: any
52+
): void {
53+
if (this.server) {
54+
this.server.server.sendLoggingMessage({ level, data });
55+
}
56+
}
57+
}

src/tools/MapboxApiBasedTool.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import type {
2-
McpServer,
3-
RegisteredTool
4-
} from '@modelcontextprotocol/sdk/server/mcp.js';
51
import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
62
import type { ZodTypeAny } from 'zod';
7-
import type { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
83
import { z } from 'zod';
4+
import { BaseTool } from './BaseTool.js';
95

106
export const OutputSchema = z.object({
117
content: z.array(
@@ -24,13 +20,12 @@ export const OutputSchema = z.object({
2420
isError: z.boolean().default(false)
2521
});
2622

27-
export abstract class MapboxApiBasedTool<InputSchema extends ZodTypeAny> {
23+
export abstract class MapboxApiBasedTool<
24+
InputSchema extends ZodTypeAny
25+
> extends BaseTool<InputSchema> {
2826
abstract readonly name: string;
2927
abstract readonly description: string;
30-
abstract readonly annotations: ToolAnnotations;
31-
32-
readonly inputSchema: InputSchema;
33-
protected server: McpServer | null = null;
28+
abstract readonly annotations: import('@modelcontextprotocol/sdk/types.js').ToolAnnotations;
3429

3530
static get mapboxAccessToken() {
3631
return process.env.MAPBOX_ACCESS_TOKEN;
@@ -41,7 +36,7 @@ export abstract class MapboxApiBasedTool<InputSchema extends ZodTypeAny> {
4136
}
4237

4338
constructor(params: { inputSchema: InputSchema }) {
44-
this.inputSchema = params.inputSchema;
39+
super(params);
4540
}
4641

4742
/**
@@ -132,33 +127,4 @@ export abstract class MapboxApiBasedTool<InputSchema extends ZodTypeAny> {
132127
_input: z.infer<InputSchema>,
133128
accessToken: string
134129
): Promise<any>;
135-
136-
/**
137-
* Installs the tool to the given MCP server.
138-
*/
139-
installTo(server: McpServer): RegisteredTool {
140-
this.server = server;
141-
return server.registerTool(
142-
this.name,
143-
{
144-
title: this.annotations.title,
145-
description: this.description,
146-
inputSchema: (this.inputSchema as unknown as z.ZodObject<any>).shape,
147-
annotations: this.annotations
148-
},
149-
(args, extra) => this.run(args, extra)
150-
);
151-
}
152-
153-
/**
154-
* Helper method to send logging messages
155-
*/
156-
protected log(
157-
level: 'debug' | 'info' | 'warning' | 'error',
158-
data: any
159-
): void {
160-
if (this.server) {
161-
this.server.server.sendLoggingMessage({ level, data });
162-
}
163-
}
164130
}

src/tools/version-tool/VersionTool.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
import type {
2-
McpServer,
3-
RegisteredTool
4-
} from '@modelcontextprotocol/sdk/server/mcp.js';
5-
import type { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
61
import { z } from 'zod';
2+
import { BaseTool } from '../BaseTool.js';
73
import { getVersionInfo } from '../../utils/versionUtils.js';
84

95
const InputSchema = z.object({});
106

11-
export class VersionTool {
7+
export class VersionTool extends BaseTool<typeof InputSchema> {
128
readonly name = 'version_tool';
139
readonly description =
1410
'Get the current version information of the MCP server';
15-
readonly inputSchema = InputSchema;
16-
readonly annotations: ToolAnnotations = {
11+
readonly annotations = {
1712
title: 'Version Information Tool',
1813
readOnlyHint: true,
1914
destructiveHint: false,
2015
idempotentHint: true,
2116
openWorldHint: false
2217
};
2318

24-
private server: McpServer | null = null;
19+
constructor() {
20+
super({ inputSchema: InputSchema });
21+
}
2522

2623
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2724
async run(_rawInput: unknown): Promise<{
@@ -65,24 +62,4 @@ export class VersionTool {
6562
};
6663
}
6764
}
68-
69-
installTo(server: McpServer): RegisteredTool {
70-
this.server = server;
71-
return server.registerTool(
72-
this.name,
73-
{
74-
title: this.annotations.title,
75-
description: this.description,
76-
inputSchema: this.inputSchema.shape,
77-
annotations: this.annotations
78-
},
79-
this.run.bind(this)
80-
);
81-
}
82-
83-
private log(level: 'debug' | 'info' | 'warning' | 'error', data: any): void {
84-
if (this.server) {
85-
this.server.server.sendLoggingMessage({ level, data });
86-
}
87-
}
8865
}

0 commit comments

Comments
 (0)