Skip to content

Commit c95fbf4

Browse files
authored
Add files via upload
1 parent 457c882 commit c95fbf4

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

07 - building clients/solutions/typescript/src/client-llm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ main().catch((error) => {
160160

161161
});
162162

163+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
2+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3+
import { z } from "zod";
4+
5+
// Create an MCP server
6+
const server = new McpServer({
7+
name: "Demo",
8+
version: "1.0.0"
9+
});
10+
11+
/*
12+
- Ask for products of a certain category.
13+
- Adding products to the cart all done using natural language.
14+
*/
15+
16+
const products = [{
17+
id: 1,
18+
name: "Red ridinghood",
19+
category: "books"
20+
},
21+
{
22+
id: 2,
23+
name: "Three musketeers",
24+
category: "books"
25+
},
26+
{
27+
id: 3,
28+
name: "White album",
29+
category: "music"
30+
}]
31+
32+
let cart: typeof products = []
33+
34+
// Add a tool
35+
server.tool(
36+
"products",
37+
"get products by category",
38+
{
39+
category: z.string().optional()
40+
},
41+
async ({ category }) => {
42+
category = String(category).toLowerCase();
43+
console.log("Server: ", category);
44+
45+
let filteredProducts = category ? products.filter(p => p.category === category): products;
46+
47+
return {
48+
content: [{ type: "text", text: filteredProducts.map(p => p.name).join(",") }]
49+
};
50+
}
51+
);
52+
53+
server.tool(
54+
"cart-list",
55+
"get products in cart",
56+
{},
57+
async () => {
58+
return {
59+
content: [{ type: "text", text: cart.map(p => p.name).join(", ") || "Cart is empty" }]
60+
};
61+
}
62+
);
63+
64+
server.tool(
65+
"cart-add",
66+
"Adding products to cart",
67+
{
68+
title: z.string().optional()
69+
},
70+
async ({ title }) => {
71+
title = String(title).toLowerCase();
72+
// TODO, use the input that's present, if neither is present answer with error
73+
let foundProduct = products.find(p => p.name.toLowerCase().startsWith(title));
74+
75+
let response = foundProduct ? `Added ${foundProduct.name} to cart` : `Product not found`;
76+
if (foundProduct) {
77+
// TODO, use the input that's present, if neither is present answer with error
78+
cart.push(foundProduct);
79+
}
80+
81+
return {
82+
content: [{ type: "text", text: response }]
83+
};
84+
}
85+
);
86+
87+
88+
// Add an addition tool
89+
server.tool("add",
90+
{ a: z.number(), b: z.number() },
91+
async ({ a, b }) => ({
92+
content: [{ type: "text", text: String(a + b) }]
93+
})
94+
);
95+
96+
// Start receiving messages on stdin and sending messages on stdout
97+
const transport = new StdioServerTransport();
98+
await server.connect(transport);

0 commit comments

Comments
 (0)