feat(bedrock-kb-retrieval-mcp-server): expose document metadata and add metadata_filter to QueryKnowledgeBases - #4330
Open
felipedbene wants to merge 3 commits into
Open
Conversation
…dd metadata_filter to QueryKnowledgeBases - Include the Bedrock Retrieve API's per-result metadata object in each document emitted by query_knowledge_base (previously only content, location, and score were returned) - Add optional metadata_filter parameter to query_knowledge_base and the QueryKnowledgeBases tool: a raw passthrough of the Bedrock RetrievalFilter schema into retrievalConfiguration.vectorSearchConfiguration.filter - Compose metadata_filter with the existing data_source_ids filter via andAll when both are provided; a single condition stays bare Closes awslabs#2935
Contributor
|
This pull request is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2935
Description
Two backward-compatible changes to
src/bedrock-kb-retrieval-mcp-server, as discussed in #2935:Expose document
metadatain results. The BedrockRetrieveAPI returns ametadataobject per result (systemx-amz-bedrock-kb-*attributes plus any custom attributes from.metadata.jsonsidecars), butknowledgebases/retrieval.pypreviously built the emitted document fromcontent,location, andscoreonly. Each emitted document now includes'metadata': result.get('metadata', {}). Existing callers just see one additional key.Add optional
metadata_filterparameter toQueryKnowledgeBases. A raw passthrough of the documentedRetrievalFilterschema intoretrievalConfiguration.vectorSearchConfiguration.filter. This imposes no schema opinions and supports any filter expression the Bedrock API supports (equals,notEquals,greaterThan,lessThan,in,startsWith,andAll,orAll, etc.). The existingdata_source_idsfilter logic was refactored so that when both are provided they are composed viaandAll; a single condition stays bare, preserving the exact request shape emitted today fordata_source_ids-only calls.The composition respects the Retrieve API's documented one-level filter embedding limit: a top-level
andAllfilter has the data-source condition merged into its member list (AND is associative, so this preserves both semantics and depth); any other filter is wrapped together with the data-source condition in a newandAll. The one unresolvable corner — a top-levelorAllthat already contains embedded filter groups, combined withdata_source_ids— raises aValueErrorwith an actionable message rather than sending a request the API would reject. An emptymetadata_filterobject also raises an actionableValueError(the API rejects an emptyRetrievalFiltertagged union, so passing it through would only produce an opaque botocoreParamValidationError), and a vacuous{'andAll': []}merged with the data-source condition degrades to the bare condition to respect the API's two-member minimum forandAll.Validation
metadata_filteralone;data_source_idsalone; both combined underandAll).{}when absent from the API response, filter passthrough alone (noandAllwrapper), empty-object filter rejected with an actionableValueError,data_source_idsalone (unchanged request shape), simple filter +data_source_idswrapped inandAll, top-levelandAllmerged in place (depth preserved), vacuous{'andAll': []}degrading to the bare data-source condition, flatorAllwrapped correctly, and the nested-orAll+data_source_idsValueError.ruff check/ruff formatclean,pyright0 errors.Type of change
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.