Skip to main content
Glama
liiklin

Dify Knowledge MCP Server

by liiklin

query_dify_knowledge

Search and retrieve information from a Dify knowledge base using natural language queries to find relevant answers and data.

Instructions

Query Dify knowledge base with a search query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe search query to find relevant knowledge
top_kNoNumber of results to return (default: 3)
search_methodNoSearch method to usehybrid_search
reranking_enableNoEnable reranking of search results

Implementation Reference

  • index.js:52-106 (handler)
    Core handler function that executes the tool logic by calling the Dify API with the provided query and options, processes the response, and returns relevant knowledge entries.
    async function queryDifyKnowledge(query, options = {}) { if (!query) { throw new McpError(ErrorCode.InvalidRequest, 'Query parameter is required'); } if (!DIFY_API_URL || !DIFY_API_KEY) { throw new McpError(ErrorCode.InvalidRequest, 'Please configure DIFY_API_URL and DIFY_API_KEY in .env file, environment variables, or command line arguments'); } try { const headers = { 'Authorization': `Bearer ${DIFY_API_KEY}`, 'Content-Type': 'application/json' }; const payload = { query: query, retrieval_model: { search_method: options.search_method || 'hybrid_search', reranking_enable: options.reranking_enable || true, reranking_mode: options.reranking_mode || 'reranking_model', reranking_model: { reranking_provider_name: '', reranking_model_name: '' }, weights: null, top_k: options.top_k || 3, score_threshold_enabled: options.score_threshold_enabled || false, score_threshold: options.score_threshold || null } }; const response = await axios.post(DIFY_API_URL, payload, { headers }); // 提取并返回records中的content字段 const contents = response.data.records.map(record => ({ content: record.segment.content, score: record.score, position: record.position })); return { success: true, data: contents, total_count: response.data.records.length }; } catch (error) { console.error('Error calling Dify API:', error.response ? error.response.data : error.message); throw new McpError( ErrorCode.InternalError, `Failed to retrieve knowledge from Dify: ${error.response ? error.response.data.message : error.message}` ); } }
  • Input schema definition for the query_dify_knowledge tool, specifying parameters like query, top_k, search_method, and reranking_enable.
    inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query to find relevant knowledge" }, top_k: { type: "number", description: "Number of results to return (default: 3)", default: 3 }, search_method: { type: "string", description: "Search method to use", enum: ["semantic_search", "full_text_search", "hybrid_search"], default: "hybrid_search" }, reranking_enable: { type: "boolean", description: "Enable reranking of search results", default: true } }, required: ["query"] }
  • index.js:112-141 (registration)
    Tool registration in the ListTools response, defining the name, description, and input schema.
    { name: "query_dify_knowledge", description: "Query Dify knowledge base with a search query", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query to find relevant knowledge" }, top_k: { type: "number", description: "Number of results to return (default: 3)", default: 3 }, search_method: { type: "string", description: "Search method to use", enum: ["semantic_search", "full_text_search", "hybrid_search"], default: "hybrid_search" }, reranking_enable: { type: "boolean", description: "Enable reranking of search results", default: true } }, required: ["query"] } },
  • MCP CallToolRequest handler case for query_dify_knowledge, which invokes the core queryDifyKnowledge function and formats the response as MCP content.
    case "query_dify_knowledge": try { const result = await queryDifyKnowledge(args.query, { top_k: args.top_k, search_method: args.search_method, reranking_enable: args.reranking_enable }); return { content: [ { type: "text", text: `Found ${result.total_count} relevant knowledge entries:\n\n` + result.data.map((item, index) => `${index + 1}. (Score: ${item.score})\n${item.content}\n` ).join('\n') } ] }; } catch (error) { return { content: [ { type: "text", text: `Error: ${error.message}` } ], isError: true }; }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/liiklin/dify-knowledge-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server