Skip to main content
Glama
sammcj

AWS Knowledge Base Retrieval MCP Server

by sammcj

retrieve_from_aws_kb

Retrieve information from AWS Knowledge Bases using queries to access documentation and resources for AWS services and solutions.

Instructions

Performs retrieval from the AWS Knowledge Base using the provided query and Knowledge Base ID.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe query to perform retrieval on
knowledgeBaseIdYesThe ID of the AWS Knowledge Base
nNoNumber of results to retrieve

Implementation Reference

  • Handles the tool call for 'retrieve_from_aws_kb', resolves knowledge base ID from arguments or environment, invokes the retrieval helper, handles errors, and formats the MCP response with text context and JSON sources.
    if (name === "retrieve_from_aws_kb") {
      const { query, knowledgeBaseId, n = 3 } = args as Record<string, any>;
    
      // Determine which knowledge base ID to use
      let actualKnowledgeBaseId = knowledgeBaseId;
    
      // If no ID provided but we have configured IDs, use the first one
      if (!actualKnowledgeBaseId && configuredKnowledgeBaseIds.length > 0) {
        actualKnowledgeBaseId = configuredKnowledgeBaseIds[0];
        console.error(`Using configured knowledge base ID: ${actualKnowledgeBaseId}`);
      }
    
      // If still no ID available, return an error
      if (!actualKnowledgeBaseId) {
        return {
          content: [{
            type: "text",
            text: "No knowledge base ID provided. Either include a knowledgeBaseId in your request or configure AWS_KB_IDS in the environment."
          }],
          isError: true,
        };
      }
    
      try {
        const result = await retrieveContext(query, actualKnowledgeBaseId, n);
        if (result.isRagWorking) {
          // Format RAG sources for readability
          const formattedSources = result.ragSources.map((source, index) => {
            return `Source ${index + 1}: ${source.fileName} (score: ${source.score.toFixed(3)})\n${source.snippet}`;
          }).join('\n\n');
    
          return {
            content: [
              {
                type: "text",
                text: result.context
              },
              {
                type: "json",
                json: {
                  ragSources: result.ragSources
                }
              }
            ],
          };
        } else {
          return {
            content: [{ type: "text", text: "Retrieval failed or returned no results." }],
          };
        }
      } catch (error) {
        return {
          content: [{ type: "text", text: `Error occurred: ${error}` }],
        };
      }
    } else {
  • Defines the tool metadata including name, description, input schema with properties for query, optional knowledgeBaseId, and number of results.
    const RETRIEVAL_TOOL: Tool = {
      name: "retrieve_from_aws_kb",
      description: "Performs retrieval from the AWS Knowledge Base using the provided query and Knowledge Base ID.",
      inputSchema: {
        type: "object",
        properties: {
          query: { type: "string", description: "The query to perform retrieval on" },
          knowledgeBaseId: {
            type: "string",
            description: configuredKnowledgeBaseIds.length > 0
              ? "The ID of the AWS Knowledge Base (optional if configured via AWS_KB_IDS)"
              : "The ID of the AWS Knowledge Base"
          },
          n: { type: "number", default: 3, description: "Number of results to retrieve" },
        },
        required: configuredKnowledgeBaseIds.length > 0 ? ["query"] : ["query", "knowledgeBaseId"],
      },
    };
  • index.ts:142-144 (registration)
    Registers the 'retrieve_from_aws_kb' tool by including it in the tools list returned by ListToolsRequestSchema.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({
      tools: [RETRIEVAL_TOOL],
    }));
  • Core helper function that executes the AWS Bedrock RetrieveCommand, processes retrieval results into context string and RAG sources with metadata.
    async function retrieveContext(
      query: string,
      knowledgeBaseId: string,
      n: number = 3
    ): Promise<{
      context: string;
      isRagWorking: boolean;
      ragSources: RAGSource[];
    }> {
      try {
        if (!knowledgeBaseId) {
          console.error("knowledgeBaseId is not provided");
          return {
            context: "",
            isRagWorking: false,
            ragSources: [],
          };
        }
    
        const input: RetrieveCommandInput = {
          knowledgeBaseId: knowledgeBaseId,
          retrievalQuery: { text: query },
          retrievalConfiguration: {
            vectorSearchConfiguration: { numberOfResults: n },
          },
        };
    
        const command = new RetrieveCommand(input);
        const response = await bedrockClient.send(command);
        const rawResults = response?.retrievalResults || [];
        const ragSources: RAGSource[] = rawResults
          .filter((res) => res?.content?.text)
          .map((result, index) => {
            const uri = result?.location?.s3Location?.uri || "";
            const fileName = uri.split("/").pop() || `Source-${index}.txt`;
            return {
              id: (result.metadata?.["x-amz-bedrock-kb-chunk-id"] as string) || `chunk-${index}`,
              fileName: fileName.replace(/_/g, " ").replace(".txt", ""),
              snippet: result.content?.text || "",
              score: (result.score as number) || 0,
            };
          })
          .slice(0, 3);
    
        const context = rawResults
          .filter((res): res is { content: { text: string } } => res?.content?.text !== undefined)
          .map(res => res.content.text)
          .join("\n\n");
    
        return {
          context,
          isRagWorking: true,
          ragSources,
        };
      } catch (error) {
        console.error("RAG Error:", error);
        return { context: "", isRagWorking: false, ragSources: [] };
      }
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions retrieval but fails to describe key traits like authentication requirements, rate limits, error handling, or what the retrieval entails (e.g., returns text snippets, metadata). This leaves significant gaps in understanding how the tool behaves in practice.

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

Conciseness5/5

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

The description is a single, efficient sentence that directly states the tool's function and inputs without any fluff. It's front-loaded with the core purpose and appropriately sized for the task, making it easy to parse quickly.

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 the retrieval returns (e.g., format, content), error conditions, or operational details like latency or costs. For a tool with 3 parameters and missing structured data, this leaves the agent under-informed about critical context.

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 all parameters (query, knowledgeBaseId, n). The description adds no additional meaning beyond what's in the schema, such as examples or constraints. This meets the baseline for high schema coverage but doesn't enhance parameter understanding.

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 action ('performs retrieval') and the resource ('from the AWS Knowledge Base'), making the purpose immediately understandable. It specifies the required inputs (query and Knowledge Base ID), which helps distinguish it from generic retrieval tools. However, without sibling tools, it doesn't need to differentiate further, so it's not a perfect 5.

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, prerequisites, or contextual constraints. It merely states what the tool does without indicating appropriate scenarios or limitations, leaving the agent without usage direction.

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/sammcj/mcp-aws-kb'

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