Skip to main content
Glama
halans

Knowledge Base MCP Server

by halans

search_knowledge

Search a custom knowledge base to find relevant information chunks based on your query, returning the most pertinent results for your needs.

Instructions

Search the knowledge base for relevant information. Returns the most relevant chunks based on your query.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe search query (1-500 characters)
top_kNoNumber of results to return (default: 5, max: 20)

Implementation Reference

  • The handler function `textSearch` implements the actual search logic, performing keyword matching against chunks and calculating relevance scores.
    export function textSearch(
      query: string,
      topK: number = 5
    ): SearchResult[] {
      const knowledge = getKnowledge();
      const queryLower = query.toLowerCase();
      const queryTerms = queryLower.split(/\s+/).filter((term) => term.length > 2);
    
      const results: SearchResult[] = [];
    
      for (const chunk of knowledge.chunks) {
        const contentLower = chunk.content.toLowerCase();
        const titleLower = chunk.title.toLowerCase();
        const categoryLower = chunk.category.toLowerCase();
    
        // Calculate match score based on term frequency
        let score = 0;
        for (const term of queryTerms) {
          if (titleLower.includes(term)) score += 3; // Title matches are worth more
          if (categoryLower.includes(term)) score += 2;
          
          // Count occurrences in content (safe, no regex)
          score += countOccurrences(contentLower, term);
        }
    
        // Check for exact phrase match
        if (contentLower.includes(queryLower)) {
          score += 5;
        }
    
        if (score > 0) {
          results.push({
            id: chunk.id,
            title: chunk.title,
            category: chunk.category,
            content: chunk.content,
            score: score,
          });
        }
      }
    
      // Sort by score descending and take top K
      results.sort((a, b) => b.score - a.score);
      return results.slice(0, topK);
    }
  • src/index.ts:21-80 (registration)
    The "search_knowledge" tool is registered with the MCP server, defining its schema and calling the textSearch handler.
    server.tool(
      "search_knowledge",
      "Search the knowledge base for relevant information. Returns the most relevant chunks based on your query.",
      {
        query: z.string().min(1).max(500).describe("The search query (1-500 characters)"),
        top_k: z
          .number()
          .min(1)
          .max(20)
          .default(5)
          .describe("Number of results to return (default: 5, max: 20)"),
      },
      async ({ query, top_k }) => {
        try {
          const results: SearchResult[] = textSearch(query, top_k);
    
          if (results.length === 0) {
            return {
              content: [
                {
                  type: "text" as const,
                  text: `No results found for query: "${query}"`,
                },
              ],
            };
          }
    
          const formattedResults = results.map((result, index) => {
            return `## Result ${index + 1}: ${result.title}
    **Category:** ${result.category}
    **Chunk ID:** ${result.id}
    **Relevance Score:** ${result.score.toFixed(2)}
    
    ${result.content}
    `;
          });
    
          return {
            content: [
              {
                type: "text" as const,
                text: `Found ${results.length} relevant results for: "${query}"\n\n${formattedResults.join("\n---\n\n")}`,
              },
            ],
          };
        } catch (error) {
          const errorMessage =
            error instanceof Error ? error.message : "Unknown error";
          return {
            content: [
              {
                type: "text" as const,
                text: `Error searching knowledge base: ${errorMessage}`,
              },
            ],
            isError: true,
          };
        }
      }
    );
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It states the tool searches and returns relevant chunks, but lacks critical behavioral details: it doesn't mention if this is a read-only operation, how relevance is determined (e.g., semantic vs. keyword), whether results are paginated or limited, or any rate limits or authentication needs. The description is too vague for a tool with no annotation support.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise with two sentences that directly state the tool's function and output. It's front-loaded with the main purpose. However, the second sentence could be more specific (e.g., 'Returns the top-k most relevant text chunks'), and there's some redundancy with 'relevant' repeated.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations and no output schema, the description is incomplete. It doesn't explain what 'chunks' are (e.g., text snippets, documents), how results are formatted, or any error conditions. For a search tool with behavioral complexity, this leaves significant gaps for an AI agent to understand proper usage and expectations.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents parameters 'query' and 'top_k.' The description adds no additional meaning beyond what's in the schema (e.g., it doesn't explain what constitutes a 'chunk' or how 'top_k' affects relevance). Baseline score of 3 is appropriate as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Search the knowledge base for relevant information' (verb+resource). It distinguishes from sibling 'get_chunk' (which likely retrieves a specific chunk) and 'list_categories' (which likely lists categories). However, it doesn't specify what 'knowledge base' refers to or the nature of 'chunks,' leaving some ambiguity.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'get_chunk' or 'list_categories.' It mentions 'Returns the most relevant chunks' but doesn't clarify when searching is preferable over direct retrieval or listing. No exclusions, prerequisites, or context for usage are provided.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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/halans/local-mcp-simple'

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