query_knowledge_base
Search and retrieve relevant information from indexed documents in the MCP Knowledge Base Server by submitting queries and specifying result limits or similarity thresholds.
Instructions
查询知识库
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_results | No | 最大返回结果数 | |
| question | Yes | 查询问题 | |
| threshold | No | 相似度阈值 |
Input Schema (JSON Schema)
{
"properties": {
"max_results": {
"default": 5,
"description": "最大返回结果数",
"type": "number"
},
"question": {
"description": "查询问题",
"type": "string"
},
"threshold": {
"default": 0.1,
"description": "相似度阈值",
"type": "number"
}
},
"required": [
"question"
],
"type": "object"
}
Implementation Reference
- src/mcp-server.ts:188-216 (handler)The MCP CallToolRequestHandler case for 'query_knowledge_base'. Extracts args, calls KnowledgeBase.query, formats response with question, answer, confidence score, and source documents/snippets into a single text block.case 'query_knowledge_base': { const { question, max_results, threshold } = args as { question: string; max_results?: number; threshold?: number; }; const response = await this.knowledgeBase.query({ question, maxResults: max_results, threshold }); let resultText = `问题: ${question}\n\n答案: ${response.answer}\n\n置信度: ${(response.confidence * 100).toFixed(1)}%\n\n来源:\n`; response.sources.forEach((source, index) => { resultText += `${index + 1}. ${source.document.title} (相似度: ${(source.score * 100).toFixed(1)}%)\n`; resultText += ` 片段: ${source.snippet}\n\n`; }); return { content: [ { type: 'text', text: resultText } ] }; }
- src/mcp-server.ts:77-96 (schema)JSON Schema for the input parameters of the 'query_knowledge_base' tool, defining question (required), max_results, and threshold.inputSchema: { type: 'object', properties: { question: { type: 'string', description: '查询问题' }, max_results: { type: 'number', description: '最大返回结果数', default: 5 }, threshold: { type: 'number', description: '相似度阈值', default: 0.1 } }, required: ['question'] }
- src/mcp-server.ts:74-97 (registration)Tool registration entry in ListToolsRequestHandler response, specifying name, description, and input schema for 'query_knowledge_base'.{ name: 'query_knowledge_base', description: '查询知识库', inputSchema: { type: 'object', properties: { question: { type: 'string', description: '查询问题' }, max_results: { type: 'number', description: '最大返回结果数', default: 5 }, threshold: { type: 'number', description: '相似度阈值', default: 0.1 } }, required: ['question'] } },
- src/knowledge-base.ts:48-62 (helper)KnowledgeBase.query method: orchestrates document search using searchDocuments, generates a simple answer from top result, computes confidence; core logic delegated by the MCP handler.async query(request: QueryRequest): Promise<QueryResponse> { const { question, maxResults = this.config.maxSearchResults, threshold = this.config.similarityThreshold } = request; // 简单的关键词搜索实现 const results = this.searchDocuments(question, maxResults, threshold); // 生成答案 const answer = this.generateAnswer(question, results); return { answer, sources: results, confidence: this.calculateConfidence(results) }; }
- src/types.ts:27-31 (schema)TypeScript type definition for QueryRequest interface used in KnowledgeBase.query, matching the tool's input schema.export interface QueryRequest { question: string; maxResults?: number; threshold?: number; }