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

85 lines
2.2 KiB
Markdown

# Näidis
See on JavaScripti näidis MCP serveri jaoks.
Siin on näide tööriista registreerimisest, kus registreerime tööriista, mis teeb näidisväljakutse LLM-ile:
```javascript
this.mcpServer.tool(
'completion',
{
model: z.string(),
prompt: z.string(),
options: z.object({
temperature: z.number().optional(),
max_tokens: z.number().optional(),
stream: z.boolean().optional()
}).optional()
},
async ({ model, prompt, options }) => {
console.log(`Processing completion request for model: ${model}`);
// Validate model
if (!this.models.includes(model)) {
throw new Error(`Model ${model} not supported`);
}
// Emit event for monitoring/metrics
this.events.emit('request', {
type: 'completion',
model,
timestamp: new Date()
});
// In a real implementation, this would call an AI model
// Here we just echo back parts of the request with a mock response
const response = {
id: `mcp-resp-${Date.now()}`,
model,
text: `This is a response to: ${prompt.substring(0, 30)}...`,
usage: {
promptTokens: prompt.split(' ').length,
completionTokens: 20,
totalTokens: prompt.split(' ').length + 20
}
};
// Simulate network delay
await new Promise(resolve => setTimeout(resolve, 500));
// Emit completion event
this.events.emit('completion', {
model,
timestamp: new Date()
});
return {
content: [
{
type: 'text',
text: JSON.stringify(response)
}
]
};
}
);
```
## Paigaldamine
Käivita järgmine käsk:
```bash
npm install
```
## Käivitamine
```bash
npm start
```
---
**Lahtiütlus**:
See dokument on tõlgitud AI tõlketeenuse [Co-op Translator](https://github.com/Azure/co-op-translator) abil. Kuigi püüame tagada täpsust, palume arvestada, et automaatsed tõlked võivad sisaldada vigu või ebatäpsusi. Algne dokument selle algses keeles tuleks pidada autoriteetseks allikaks. Olulise teabe puhul soovitame kasutada professionaalset inimtõlget. Me ei vastuta selle tõlke kasutamisest tulenevate arusaamatuste või valesti tõlgenduste eest.