chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
# 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
|
||||
```
|
||||
@@ -0,0 +1,69 @@
|
||||
// mcp_calculator_server.js - Sample MCP Calculator Server implementation in JavaScript
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { z } from "zod";
|
||||
|
||||
// Create an MCP server
|
||||
const server = new McpServer({
|
||||
name: "Calculator MCP Server",
|
||||
version: "1.0.0"
|
||||
});
|
||||
|
||||
// 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) }]
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
// Connect the server using stdio transport
|
||||
const transport = new StdioServerTransport();
|
||||
server.connect(transport).catch(console.error);
|
||||
|
||||
console.log("Calculator MCP Server started");
|
||||
+1140
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "tutorial-mcp",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": ">=1.26.0",
|
||||
"zod": "^3.24.2"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": ""
|
||||
}
|
||||
Reference in New Issue
Block a user