74 lines
1.0 KiB
Markdown
74 lines
1.0 KiB
Markdown
# Sample
|
|
|
|
This is a JavaScript sample for an MCP Server
|
|
|
|
Here's what the calculator portion of it looks like:
|
|
|
|
```javascript
|
|
// Define calculator tools for each operation
|
|
server.tool(
|
|
"add",
|
|
{
|
|
a: z.number(),
|
|
b: z.number()
|
|
},
|
|
async ({ a, b }) => ({
|
|
content: [{ type: "text", text: String(a + b) }]
|
|
})
|
|
);
|
|
|
|
server.tool(
|
|
"subtract",
|
|
{
|
|
a: z.number(),
|
|
b: z.number()
|
|
},
|
|
async ({ a, b }) => ({
|
|
content: [{ type: "text", text: String(a - b) }]
|
|
})
|
|
);
|
|
|
|
server.tool(
|
|
"multiply",
|
|
{
|
|
a: z.number(),
|
|
b: z.number()
|
|
},
|
|
async ({ a, b }) => ({
|
|
content: [{ type: "text", text: String(a * b) }]
|
|
})
|
|
);
|
|
|
|
server.tool(
|
|
"divide",
|
|
{
|
|
a: z.number(),
|
|
b: z.number()
|
|
},
|
|
async ({ a, b }) => {
|
|
if (b === 0) {
|
|
return {
|
|
content: [{ type: "text", text: "Error: Cannot divide by zero" }],
|
|
isError: true
|
|
};
|
|
}
|
|
return {
|
|
content: [{ type: "text", text: String(a / b) }]
|
|
};
|
|
}
|
|
);
|
|
```
|
|
|
|
## Install
|
|
|
|
Run the following command:
|
|
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
## Run
|
|
|
|
```bash
|
|
npm start
|
|
``` |