-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
186 lines (172 loc) · 4.83 KB
/
Copy pathserver.ts
File metadata and controls
186 lines (172 loc) · 4.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { initDb } from "./db/index.js";
import { getItemById, getItems, getPurchases, getUsers } from "./db/repositories.js";
import { itemsTool } from "./tools/items.js";
import { purchasesTool } from "./tools/purchases.js";
import { usersTool } from "./tools/users.js";
initDb();
const server = new McpServer({
name: "sales-mcp",
version: "0.1.0"
});
const usersSchema = {
action: z.enum(["fetch", "add"]),
name: z.string().optional(),
email: z.string().email().optional()
};
const itemsSchema = {
action: z.enum(["fetch", "add"]),
name: z.string().optional(),
price: z.number().optional(),
imageUrl: z.string().url().optional()
};
const purchasesSchema = {
action: z.enum(["fetch", "add"]),
userId: z.number().int().optional(),
itemId: z.number().int().optional(),
quantity: z.number().int().optional()
};
server.registerTool(
"users",
{
description: "Fetch users or add a new user",
inputSchema: usersSchema
},
async (args: { action: "fetch" | "add"; name?: string; email?: string }) => {
try {
const data = await usersTool(args);
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown users tool error";
return { content: [{ type: "text", text: JSON.stringify({ error: message }) }], isError: true };
}
}
);
server.registerTool(
"items",
{
description: "Fetch items or add a new item",
inputSchema: itemsSchema
},
async (args: { action: "fetch" | "add"; name?: string; price?: number; imageUrl?: string }) => {
try {
const data = await itemsTool(args);
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown items tool error";
return { content: [{ type: "text", text: JSON.stringify({ error: message }) }], isError: true };
}
}
);
server.registerTool(
"purchases",
{
description: "Fetch purchases or add a new purchase",
inputSchema: purchasesSchema
},
async (args: {
action: "fetch" | "add";
userId?: number;
itemId?: number;
quantity?: number;
}) => {
try {
const data = await purchasesTool(args);
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown purchases tool error";
return { content: [{ type: "text", text: JSON.stringify({ error: message }) }], isError: true };
}
}
);
server.registerResource(
"users",
"sales://users",
{
title: "All users",
description: "List of every registered user in the sales database (JSON).",
mimeType: "application/json"
},
async (uri) => ({
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify({ users: getUsers() }, null, 2)
}
]
})
);
server.registerResource(
"items",
"sales://items",
{
title: "Item catalog",
description: "Full catalog of items available for purchase (JSON).",
mimeType: "application/json"
},
async (uri) => ({
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify({ items: getItems() }, null, 2)
}
]
})
);
server.registerResource(
"purchases",
"sales://purchases",
{
title: "All purchases",
description: "Every purchase recorded in the sales database (JSON).",
mimeType: "application/json"
},
async (uri) => ({
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify({ purchases: getPurchases() }, null, 2)
}
]
})
);
server.registerResource(
"item",
new ResourceTemplate("sales://items/{id}", { list: undefined }),
{
title: "Item by id",
description: "Fetch a single item by its numeric id, e.g. sales://items/1 (JSON).",
mimeType: "application/json"
},
async (uri, { id }) => {
const itemId = Number(Array.isArray(id) ? id[0] : id);
if (!Number.isInteger(itemId)) {
return {
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify({ error: "id must be an integer" })
}
]
};
}
const item = getItemById(itemId);
return {
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify(item ?? { error: "item not found", id: itemId }, null, 2)
}
]
};
}
);
const transport = new StdioServerTransport();
await server.connect(transport);