77 lines
1.6 KiB
Markdown
77 lines
1.6 KiB
Markdown
# Sample
|
|
|
|
This is a JavaScript sample for an MCP Server
|
|
|
|
Here's what the calculator part 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
|
|
```
|
|
|
|
**Disclaimer**:
|
|
This document has been translated using the AI translation service [Co-op Translator](https://github.com/Azure/co-op-translator). While we strive for accuracy, please be aware that automated translations may contain errors or inaccuracies. The original document in its native language should be considered the authoritative source. For critical information, professional human translation is recommended. We are not liable for any misunderstandings or misinterpretations arising from the use of this translation. |