-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBaseTool.ts
More file actions
138 lines (126 loc) · 3.76 KB
/
Copy pathBaseTool.ts
File metadata and controls
138 lines (126 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
import {
McpServer,
RegisteredTool
} from '@modelcontextprotocol/sdk/server/mcp.js';
import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
import {
CallToolResult,
ToolAnnotations
} from '@modelcontextprotocol/sdk/types.js';
import { z, ZodTypeAny } from 'zod';
import type { ToolExecutionContext } from '../utils/tracing.js';
export abstract class BaseTool<
InputSchema extends ZodTypeAny,
OutputSchema extends ZodTypeAny = ZodTypeAny
> {
abstract readonly name: string;
abstract readonly description: string;
abstract readonly annotations: ToolAnnotations;
readonly inputSchema: InputSchema;
readonly outputSchema?: OutputSchema;
readonly meta?: {
ui?: {
resourceUri?: string;
csp?: {
connectDomains?: string[];
resourceDomains?: string[];
frameDomains?: string[];
};
};
};
protected server: McpServer | null = null;
constructor(params: {
inputSchema: InputSchema;
outputSchema?: OutputSchema;
}) {
this.inputSchema = params.inputSchema;
this.outputSchema = params.outputSchema;
}
/**
* Tool logic to be implemented by subclasses.
*/
async run(
rawInput: unknown,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
extra?: RequestHandlerExtra<any, any>
): Promise<CallToolResult> {
try {
const input = this.inputSchema.parse(rawInput);
const accessToken =
extra?.authInfo?.token || process.env.MAPBOX_ACCESS_TOKEN;
return this.execute(input, accessToken);
} catch (error) {
return {
isError: true,
content: [{ type: 'text', text: (error as Error).message }]
};
}
}
protected abstract execute(
inputSchema: z.infer<InputSchema>,
accessToken?: string,
context?: ToolExecutionContext
): Promise<CallToolResult>;
/**
* Installs the tool to the given MCP server.
*/
installTo(server: McpServer): RegisteredTool {
this.server = server;
const config: {
title?: string;
description?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputSchema?: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
outputSchema?: any;
annotations?: ToolAnnotations;
_meta?: {
ui?: {
resourceUri?: string;
csp?: {
connectDomains?: string[];
resourceDomains?: string[];
frameDomains?: string[];
};
};
};
} = {
title: this.annotations.title,
description: this.description,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputSchema: (this.inputSchema as unknown as z.ZodObject<any>).shape,
annotations: this.annotations
};
// Add outputSchema if provided
if (this.outputSchema) {
// Pass the schema shape directly - don't wrap
// The MCP SDK will validate structuredContent against this schema
config.outputSchema =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(this.outputSchema as unknown as z.ZodObject<any>).shape;
}
// Add _meta for MCP Apps support if provided (includes CSP configuration)
if (this.meta) {
config._meta = this.meta;
}
return server.registerTool(
this.name,
config,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args: any, extra: any) => this.run(args, extra)
);
}
/**
* Helper method to send logging messages
*/
protected log(
level: 'debug' | 'info' | 'warning' | 'error',
data: string | Record<string, unknown>
): void {
if (this.server?.server) {
this.server.server.sendLoggingMessage({ level, data });
}
}
}