@smithery/sdk 0.0.11 → 0.0.13
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/dist/server/builder.d.ts +35 -0
- package/dist/server/builder.js +212 -0
- package/dist/server/types.d.ts +36 -0
- package/dist/server/types.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import type { Prompt, PromptArgument, Resource, Tool, ToolParameters } from "./types.js";
|
|
3
|
+
export declare class ServerBuilder {
|
|
4
|
+
#private;
|
|
5
|
+
name: string;
|
|
6
|
+
version: string;
|
|
7
|
+
constructor(name?: string, version?: string);
|
|
8
|
+
private setupHandlers;
|
|
9
|
+
private setupErrorHandling;
|
|
10
|
+
private setupToolHandlers;
|
|
11
|
+
private setupResourceHandlers;
|
|
12
|
+
private setupPromptHandlers;
|
|
13
|
+
addTool<Params extends ToolParameters>(tool: Tool<Params>): this;
|
|
14
|
+
addResource(resource: Resource): this;
|
|
15
|
+
addPrompt<const Args extends PromptArgument[]>(prompt: Prompt<Args>): this;
|
|
16
|
+
build(): Server<{
|
|
17
|
+
method: string;
|
|
18
|
+
params?: import("zod").objectOutputType<{
|
|
19
|
+
_meta: import("zod").ZodOptional<import("zod").ZodObject<{
|
|
20
|
+
progressToken: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodString, import("zod").ZodNumber]>>;
|
|
21
|
+
}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
|
|
22
|
+
progressToken: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodString, import("zod").ZodNumber]>>;
|
|
23
|
+
}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
|
|
24
|
+
progressToken: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodString, import("zod").ZodNumber]>>;
|
|
25
|
+
}, import("zod").ZodTypeAny, "passthrough">>>;
|
|
26
|
+
}, import("zod").ZodTypeAny, "passthrough"> | undefined;
|
|
27
|
+
}, {
|
|
28
|
+
method: string;
|
|
29
|
+
params?: import("zod").objectOutputType<{
|
|
30
|
+
_meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
|
|
31
|
+
}, import("zod").ZodTypeAny, "passthrough"> | undefined;
|
|
32
|
+
}, import("zod").objectOutputType<{
|
|
33
|
+
_meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
|
|
34
|
+
}, import("zod").ZodTypeAny, "passthrough">>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _ServerBuilder_tools, _ServerBuilder_resources, _ServerBuilder_prompts;
|
|
13
|
+
// Based on https://github.com/wong2/litemcp/
|
|
14
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
15
|
+
import { CallToolRequestSchema, ErrorCode, GetPromptRequestSchema, ListPromptsRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, McpError, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
16
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
17
|
+
export class ServerBuilder {
|
|
18
|
+
constructor(name = "mcp-server", version = "1.0.0") {
|
|
19
|
+
this.name = name;
|
|
20
|
+
this.version = version;
|
|
21
|
+
_ServerBuilder_tools.set(this, void 0);
|
|
22
|
+
_ServerBuilder_resources.set(this, void 0);
|
|
23
|
+
_ServerBuilder_prompts.set(this, void 0);
|
|
24
|
+
__classPrivateFieldSet(this, _ServerBuilder_tools, [], "f");
|
|
25
|
+
__classPrivateFieldSet(this, _ServerBuilder_resources, [], "f");
|
|
26
|
+
__classPrivateFieldSet(this, _ServerBuilder_prompts, [], "f");
|
|
27
|
+
}
|
|
28
|
+
setupHandlers(server) {
|
|
29
|
+
this.setupErrorHandling(server);
|
|
30
|
+
if (__classPrivateFieldGet(this, _ServerBuilder_tools, "f").length) {
|
|
31
|
+
this.setupToolHandlers(server);
|
|
32
|
+
}
|
|
33
|
+
if (__classPrivateFieldGet(this, _ServerBuilder_resources, "f").length) {
|
|
34
|
+
this.setupResourceHandlers(server);
|
|
35
|
+
}
|
|
36
|
+
if (__classPrivateFieldGet(this, _ServerBuilder_prompts, "f").length) {
|
|
37
|
+
this.setupPromptHandlers(server);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
setupErrorHandling(server) {
|
|
41
|
+
server.onerror = (error) => {
|
|
42
|
+
console.error("[MCP Error]", error);
|
|
43
|
+
};
|
|
44
|
+
process.on("SIGINT", async () => {
|
|
45
|
+
await server.close();
|
|
46
|
+
process.exit(0);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
setupToolHandlers(server) {
|
|
50
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
51
|
+
return {
|
|
52
|
+
tools: __classPrivateFieldGet(this, _ServerBuilder_tools, "f").map((tool) => {
|
|
53
|
+
return {
|
|
54
|
+
name: tool.name,
|
|
55
|
+
description: tool.description,
|
|
56
|
+
inputSchema: tool.parameters
|
|
57
|
+
? zodToJsonSchema(tool.parameters)
|
|
58
|
+
: undefined,
|
|
59
|
+
};
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
64
|
+
const tool = __classPrivateFieldGet(this, _ServerBuilder_tools, "f").find((tool) => tool.name === request.params.name);
|
|
65
|
+
if (!tool) {
|
|
66
|
+
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`);
|
|
67
|
+
}
|
|
68
|
+
let args = undefined;
|
|
69
|
+
if (tool.parameters) {
|
|
70
|
+
const parsed = tool.parameters.safeParse(request.params.arguments);
|
|
71
|
+
if (!parsed.success) {
|
|
72
|
+
return {
|
|
73
|
+
content: [
|
|
74
|
+
{
|
|
75
|
+
type: "text",
|
|
76
|
+
text: `Invalid ${request.params.name} arguments: ${parsed.error.message}`,
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
isError: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
args = parsed.data;
|
|
83
|
+
}
|
|
84
|
+
let result;
|
|
85
|
+
try {
|
|
86
|
+
result = await tool.execute(args);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
return {
|
|
90
|
+
content: [{ type: "text", text: `Error: ${error}` }],
|
|
91
|
+
isError: true,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (typeof result === "string") {
|
|
95
|
+
return {
|
|
96
|
+
content: [{ type: "text", text: result }],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
setupResourceHandlers(server) {
|
|
105
|
+
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
106
|
+
return {
|
|
107
|
+
resources: __classPrivateFieldGet(this, _ServerBuilder_resources, "f").map((resource) => {
|
|
108
|
+
return {
|
|
109
|
+
uri: resource.uri,
|
|
110
|
+
name: resource.name,
|
|
111
|
+
mimeType: resource.mimeType,
|
|
112
|
+
};
|
|
113
|
+
}),
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
117
|
+
const resource = __classPrivateFieldGet(this, _ServerBuilder_resources, "f").find((resource) => resource.uri === request.params.uri);
|
|
118
|
+
if (!resource) {
|
|
119
|
+
throw new McpError(ErrorCode.MethodNotFound, `Unknown resource: ${request.params.uri}`);
|
|
120
|
+
}
|
|
121
|
+
let result;
|
|
122
|
+
try {
|
|
123
|
+
result = await resource.load();
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
throw new McpError(ErrorCode.InternalError, `Error reading resource: ${error}`, {
|
|
127
|
+
uri: resource.uri,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
contents: [
|
|
132
|
+
{
|
|
133
|
+
uri: resource.uri,
|
|
134
|
+
mimeType: resource.mimeType,
|
|
135
|
+
...result,
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
setupPromptHandlers(server) {
|
|
142
|
+
server.setRequestHandler(ListPromptsRequestSchema, async () => {
|
|
143
|
+
return {
|
|
144
|
+
prompts: __classPrivateFieldGet(this, _ServerBuilder_prompts, "f").map((prompt) => {
|
|
145
|
+
return {
|
|
146
|
+
name: prompt.name,
|
|
147
|
+
description: prompt.description,
|
|
148
|
+
arguments: prompt.arguments,
|
|
149
|
+
};
|
|
150
|
+
}),
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
154
|
+
const prompt = __classPrivateFieldGet(this, _ServerBuilder_prompts, "f").find((prompt) => prompt.name === request.params.name);
|
|
155
|
+
if (!prompt) {
|
|
156
|
+
throw new McpError(ErrorCode.MethodNotFound, `Unknown prompt: ${request.params.name}`);
|
|
157
|
+
}
|
|
158
|
+
const args = request.params.arguments;
|
|
159
|
+
if (prompt.arguments) {
|
|
160
|
+
for (const arg of prompt.arguments) {
|
|
161
|
+
if (arg.required && !(args && arg.name in args)) {
|
|
162
|
+
throw new McpError(ErrorCode.InvalidRequest, `Missing required argument: ${arg.name}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let result;
|
|
167
|
+
try {
|
|
168
|
+
result = await prompt.load(args);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
throw new McpError(ErrorCode.InternalError, `Error loading prompt: ${error}`);
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
description: prompt.description,
|
|
175
|
+
messages: [
|
|
176
|
+
{
|
|
177
|
+
role: "user",
|
|
178
|
+
content: { type: "text", text: result },
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
addTool(tool) {
|
|
185
|
+
__classPrivateFieldGet(this, _ServerBuilder_tools, "f").push(tool);
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
addResource(resource) {
|
|
189
|
+
__classPrivateFieldGet(this, _ServerBuilder_resources, "f").push(resource);
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
addPrompt(prompt) {
|
|
193
|
+
__classPrivateFieldGet(this, _ServerBuilder_prompts, "f").push(prompt);
|
|
194
|
+
return this;
|
|
195
|
+
}
|
|
196
|
+
build() {
|
|
197
|
+
const capabilities = {};
|
|
198
|
+
if (__classPrivateFieldGet(this, _ServerBuilder_tools, "f").length) {
|
|
199
|
+
capabilities.tools = {};
|
|
200
|
+
}
|
|
201
|
+
if (__classPrivateFieldGet(this, _ServerBuilder_resources, "f").length) {
|
|
202
|
+
capabilities.resources = {};
|
|
203
|
+
}
|
|
204
|
+
if (__classPrivateFieldGet(this, _ServerBuilder_prompts, "f").length) {
|
|
205
|
+
capabilities.prompts = {};
|
|
206
|
+
}
|
|
207
|
+
const server = new Server({ name: this.name, version: this.version }, { capabilities });
|
|
208
|
+
this.setupHandlers(server);
|
|
209
|
+
return server;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
_ServerBuilder_tools = new WeakMap(), _ServerBuilder_resources = new WeakMap(), _ServerBuilder_prompts = new WeakMap();
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
export type ToolParameters = z.ZodTypeAny;
|
|
3
|
+
export interface Tool<Params extends ToolParameters = ToolParameters> {
|
|
4
|
+
name: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
parameters?: Params;
|
|
7
|
+
execute: (args: z.infer<Params>) => Promise<any>;
|
|
8
|
+
}
|
|
9
|
+
export interface Resource {
|
|
10
|
+
uri: string;
|
|
11
|
+
name: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
mimeType?: string;
|
|
14
|
+
load: () => Promise<{
|
|
15
|
+
text: string;
|
|
16
|
+
} | {
|
|
17
|
+
blob: string;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export type PromptArgument = Readonly<{
|
|
21
|
+
name: string;
|
|
22
|
+
description?: string;
|
|
23
|
+
required?: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
type ArgumentsToObject<T extends PromptArgument[]> = {
|
|
26
|
+
[K in T[number]["name"]]: Extract<T[number], {
|
|
27
|
+
name: K;
|
|
28
|
+
}>["required"] extends true ? string : string | undefined;
|
|
29
|
+
};
|
|
30
|
+
export interface Prompt<Arguments extends PromptArgument[] = PromptArgument[], Args = ArgumentsToObject<Arguments>> {
|
|
31
|
+
name: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
arguments?: Arguments;
|
|
34
|
+
load: (args: Args) => Promise<string>;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|