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 exampleUsage example的直接链接
import { createBedrockKBTool } from '@mastra/rag'
const kbTool = createBedrockKBTool({
knowledgeBaseId: 'YOUR_KB_ID',
region: 'us-west-2',
numberOfResults: 5,
useAgenticRetrieval: true,
})
With an AgentWith 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 },
})
ParametersParameters的直接链接
knowledgeBaseId:
region?:
numberOfResults?:
useAgenticRetrieval?:
userId?:
Input SchemaInput Schema的直接链接
The tool accepts the following input when called by an agent:
queryText:
Output SchemaOutput Schema的直接链接
The tool returns an object with:
results:
BedrockKBResultBedrockKBResult的直接链接
| 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, which the agentic API omits from result items. |
metadata | Record<string, unknown> | Additional metadata from the retrieval result. |
Retrieval ModesRetrieval Modes的直接链接
Agentic Retrieval (default)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 RetrievalStandard Managed Retrieval的直接链接
When useAgenticRetrieval is false, the tool uses RetrieveCommand with managedSearchConfiguration for direct single-pass retrieval.
User-based access controlUser-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 PermissionsRequired IAM Permissions的直接链接
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["bedrock:Retrieve", "bedrock:AgenticRetrieveStream"],
"Resource": "arn:aws:bedrock:*:*:knowledge-base/*"
}
]
}
SDK RequirementsSDK Requirements的直接链接
@aws-sdk/client-bedrock-agent-runtime>= 3.1000 (AgenticRetrieveStreamCommand requires ~3.1000+)