Files
2026-07-13 13:31:35 +08:00

77 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Exemple
Ceci est un exemple JavaScript pour un serveur MCP
Voici à quoi ressemble la partie calculatrice :
```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) }]
};
}
);
```
## Installation
Exécutez la commande suivante :
```bash
npm install
```
## Exécution
```bash
npm start
```
**Avertissement** :
Ce document a été traduit à laide du service de traduction automatique [Co-op Translator](https://github.com/Azure/co-op-translator). Bien que nous nous efforcions dassurer lexactitude, veuillez noter que les traductions automatiques peuvent contenir des erreurs ou des inexactitudes. Le document original dans sa langue dorigine doit être considéré comme la source faisant foi. Pour les informations critiques, une traduction professionnelle réalisée par un humain est recommandée. Nous déclinons toute responsabilité en cas de malentendus ou de mauvaises interprétations résultant de lutilisation de cette traduction.