UNPKG

mcp-server-dify

Version:

MCP server for dify

138 lines (119 loc) 3.99 kB
#!/usr/bin/env node import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { CallToolRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js"; import axios from 'axios'; const config = { server: { name: "mcp-server/dify", version: "0.1.0", }, dify: { host: process.env.DIFY_HOST || "https://api.dify.ai/v1", token: process.env.DIFY_TOKEN || "", } }; const server = new Server(config.server, { capabilities: { resources: {}, tools: {}, }, }); async function chat_message(query, inputs, responseMode, user, conversationId) { try { const response = await axios.post(`${config.dify.host}/chat-messages`, { query: query, inputs: inputs, response_mode: responseMode, user: user, conversation_id: conversationId, }, { headers: { 'Authorization': `Bearer ${config.dify.token}`, 'Content-Type': 'application/json', }, }); const result = response.data; return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], isError: false, }; } finally { } } export { chat_message }; server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: "chat_message", description: "Run a dify chat message tool", inputSchema: { type: "object", properties: { query: { type: "string", default: "",description: "The query to send to the chat message tool" }, inputs: { type: "object", default: {}, description: "The inputs to send to the chat message tool" }, responseMode: { type: "string", enum: ["streaming","blocking"], default: "streaming", description: "The response mode to use for the chat message tool" }, user: { type: "string" , description: "The user to send to the chat message tool" }, conversationId: { type: "string", default: "", description: "The conversationId to send to the chat message tool" }, }, required: ["query", "inputs", "responseMode", "user"] }, } ], })); server.setRequestHandler(CallToolRequestSchema, async (request) => { const query = request.params.arguments?.query; const inputs = request.params.arguments?.inputs; const responseMode = request.params.arguments?.responseMode; const user = request.params.arguments?.user; const conversationId = request.params.arguments?.conversationId; switch (request.params.name) { case "chat_message": return chat_message(query, inputs, responseMode, user, conversationId); break; default: throw new Error(`Unknown tool: ${request.params.name}`); } }); // 服务器设置 async function runServer() { const transport = new StdioServerTransport(); await server.connect(transport); } const shutdown = async (signal) => { console.log(`Received ${signal}. Shutting down...`); try { } catch (err) { console.error("Error:", err); throw err; } }; process.on("SIGINT", async () => { try { await shutdown("SIGINT"); process.exit(0); } catch (err) { process.exit(1); } }); process.on("SIGTERM", async () => { try { await shutdown("SIGTERM"); process.exit(0); } catch (err) { process.exit(1); } }); runServer().catch((error) => { console.error("Server error:", error); process.exit(1); });