wujie-upload-file-server
Version:
111 lines (110 loc) • 3.73 kB
JavaScript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { ListToolsRequestSchema, CallToolRequestSchema, ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
import dotenv from "dotenv";
dotenv.config();
function getApiKey() {
const apiKey = process.env.WUJIEAI_API_KEY;
if (!apiKey) {
console.error("WUJIEAI_API_KEY environment variable is not set");
process.exit(1);
}
return apiKey;
}
// 无界AI配置
const WUJIEAI_API_CONFIG = {
BASE_URL: 'https://pref-gate.wujieai.com',
API_KEY: getApiKey()
};
class WujieUploadFileMcpServer {
server;
constructor() {
this.server = new Server({
name: "wujie-uploadfile-mcp-server",
version: "1.0.0"
}, {
capabilities: {
tools: {}
}
});
this.setupHandlers();
this.setupErrorHandling();
}
setupHandlers() {
// 工具列表
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
this.uploadFile()
]
}));
// 工具处理器
this.server.setRequestHandler(CallToolRequestSchema, async (req) => {
switch (req.params.name) {
case 'upload_file':
return this.handleUploadFile(req);
default:
throw new McpError(ErrorCode.MethodNotFound, `未知工具: ${req.params.name}`);
}
});
}
uploadFile() {
return {
name: "upload_file",
description: "上传文件",
inputSchema: {
type: "object",
properties: {
file: {
type: "string",
description: "文件"
}
},
required: ["file"]
}
};
}
async handleUploadFile(request) {
const params = request.params.arguments;
// 打印完整请求信息
console.log('=== 上传文件请求详情 ===');
console.log('请求方法:', request.method);
console.log('请求ID:', request.id);
console.log('工具名称:', request.params.name);
console.log('完整参数:', JSON.stringify(request.params, null, 2));
// 返回一个简单的响应,因为当前没有实际的文件处理逻辑
return {
contents: [{
type: 'text',
text: '文件上传请求已接收,参数已打印到控制台'
}]
};
}
// 处理上传的文件
async processUploadedFile(buffer, mimeType, fileName) {
console.log(`处理上传文件: 名称=${fileName}, 类型=${mimeType}, 大小=${buffer.length}字节`);
return {
contents: [{
type: 'text',
text: `成功接收文件: ${fileName} (${mimeType}, ${buffer.length}字节)`
}]
};
}
// 错误处理(与示例保持相同结构)
setupErrorHandling() {
this.server.onerror = (error) => {
console.error("[MCP Error]", error);
};
process.on('SIGINT', async () => {
await this.server.close();
process.exit(0);
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.log("无界上传文件 MCP服务已启动");
}
}
// 启动服务
new WujieUploadFileMcpServer().run().catch(console.error);