createBedrockKBTool()
The createBedrockKBTool() function creates a tool that retrieves relevant documents from an Amazon Bedrock Knowledge Base. It supports both managed search configuration and agentic retrieval (query decomposition and managed reranking) with automatic fallback to standard retrieval.
Usage exampleDirect link to Usage example
import { createBedrockKBTool } from '@mastra/rag'
const kbTool = createBedrockKBTool({
knowledgeBaseId: 'YOUR_KB_ID',
region: 'us-west-2',
numberOfResults: 5,
useAgenticRetrieval: true,
})
With an AgentDirect link to With an Agent
import { Agent } from '@mastra/core/agent'
import { createBedrockKBTool } from '@mastra/rag'
const kbTool = createBedrockKBTool({
knowledgeBaseId: 'YOUR_KB_ID',
})
const agent = new Agent({
name: 'KnowledgeAssistant',
instructions: 'Use the knowledge base tool to answer questions.',
model: myModel,
tools: { kb: kbTool },
})
ParametersDirect link to Parameters
knowledgeBaseId:
region?:
numberOfResults?:
useAgenticRetrieval?:
userId?:
Input SchemaDirect link to Input Schema
The tool accepts the following input when called by an agent:
queryText:
Output SchemaDirect link to Output Schema
The tool returns an object with:
results:
BedrockKBResultDirect link to BedrockKBResult
| Field | Type | Description |
|---|---|---|
content | string | The text content of the retrieved passage. |
source | string | undefined | The source URI when Bedrock provides one. Agentic retrieval only includes this field when the result metadata contains _source_uri. |
score | number | undefined | The relevance score returned by standard retrieval. The agentic API doesn't return a score for result items. |
metadata | Record<string, unknown> | Additional metadata from the retrieval result. |
Retrieval ModesDirect link to Retrieval Modes
Agentic Retrieval (default)Direct link to Agentic Retrieval (default)
When useAgenticRetrieval is true (default), the tool uses AgenticRetrieveStreamCommand which:
- Decomposes complex queries into sub-queries
- Retrieves across multiple passes
- Applies managed reranking for better results
If agentic retrieval fails (e.g., older SDK, permissions), it automatically falls back to standard managed retrieval.
Standard Managed RetrievalDirect link to Standard Managed Retrieval
When useAgenticRetrieval is false, the tool uses RetrieveCommand with managedSearchConfiguration for direct single-pass retrieval.
User-based access controlDirect link to User-based access control
Set userId in the Mastra request context to forward it as the Bedrock userContext.userId. This supports knowledge bases that enforce document-level access control. The request context value overrides the default userId configured on the tool.
import { RequestContext } from '@mastra/core/request-context'
const requestContext = new RequestContext()
requestContext.set('userId', 'user-123')
await agent.generate('Find my private documents', { requestContext })
Required IAM PermissionsDirect link to Required IAM Permissions
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["bedrock:Retrieve", "bedrock:AgenticRetrieveStream"],
"Resource": "arn:aws:bedrock:*:*:knowledge-base/*"
}
]
}
SDK RequirementsDirect link to SDK Requirements
@aws-sdk/client-bedrock-agent-runtime>= 3.1000 (AgenticRetrieveStreamCommand requires ~3.1000+)