示例
这是一个用于 MCP 服务器的 Typescript 示例
下面是一个工具创建的例子:
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)
}
]
};
}
);
安装
运行以下命令:
npm install
运行
npm start
免责声明:
本文件使用 AI 翻译服务 Co-op Translator 进行翻译。虽然我们力求准确,但请注意,自动翻译可能包含错误或不准确之处。原始文件的母语版本应被视为权威来源。对于重要信息,建议使用专业人工翻译。对于因使用本翻译而产生的任何误解或误释,我们概不负责。