77 lines
2.3 KiB
Markdown
77 lines
2.3 KiB
Markdown
# ตัวอย่าง
|
|
|
|
นี่คือตัวอย่าง JavaScript สำหรับ MCP Server
|
|
|
|
ส่วนของเครื่องคิดเลขมีลักษณะดังนี้:
|
|
|
|
```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) }]
|
|
};
|
|
}
|
|
);
|
|
```
|
|
|
|
## ติดตั้ง
|
|
|
|
รันคำสั่งต่อไปนี้:
|
|
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
## รัน
|
|
|
|
```bash
|
|
npm start
|
|
```
|
|
|
|
**ข้อจำกัดความรับผิดชอบ**:
|
|
เอกสารนี้ได้รับการแปลโดยใช้บริการแปลภาษาอัตโนมัติ [Co-op Translator](https://github.com/Azure/co-op-translator) แม้เราจะพยายามให้ความถูกต้องสูงสุด แต่โปรดทราบว่าการแปลอัตโนมัติอาจมีข้อผิดพลาดหรือความไม่ถูกต้อง เอกสารต้นฉบับในภาษาต้นทางถือเป็นแหล่งข้อมูลที่เชื่อถือได้ สำหรับข้อมูลที่สำคัญ ขอแนะนำให้ใช้บริการแปลโดยผู้เชี่ยวชาญมนุษย์ เราไม่รับผิดชอบต่อความเข้าใจผิดหรือการตีความผิดใด ๆ ที่เกิดจากการใช้การแปลนี้ |