Skip to main content
Glama
liiklin

Dify Knowledge MCP Server

by liiklin

query_dify_knowledge

Retrieve relevant information from Dify knowledge bases using natural language queries. Specify search methods, result count, and enable reranking for precise, efficient results.

Instructions

Query Dify knowledge base with a search query

Input Schema

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

Implementation Reference

  • index.js:52-106 (handler)
    The core handler function that implements the logic for querying the Dify knowledge base via API, including payload construction, axios POST request, response parsing, and error handling.
    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 defining the parameters for the query_dify_knowledge tool, including query (required), 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)
    Registration of the query_dify_knowledge tool in the ListTools response, including 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 CallTool handler case for query_dify_knowledge, which invokes the 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

Related 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