83 lines
2.1 KiB
Markdown
83 lines
2.1 KiB
Markdown
# 示例
|
||
|
||
这是一个用于 MCP 服务器的 JavaScript 示例
|
||
|
||
下面是一个工具注册的例子,我们注册了一个对 LLM 进行模拟调用的工具:
|
||
|
||
```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)
|
||
}
|
||
]
|
||
};
|
||
}
|
||
);
|
||
```
|
||
|
||
## 安装
|
||
|
||
运行以下命令:
|
||
|
||
```bash
|
||
npm install
|
||
```
|
||
|
||
## 运行
|
||
|
||
```bash
|
||
npm start
|
||
```
|
||
|
||
**免责声明**:
|
||
本文件使用 AI 翻译服务 [Co-op Translator](https://github.com/Azure/co-op-translator) 进行翻译。虽然我们力求准确,但请注意,自动翻译可能包含错误或不准确之处。原始文件的母语版本应被视为权威来源。对于重要信息,建议采用专业人工翻译。我们不对因使用本翻译而产生的任何误解或误释承担责任。 |