Tool: retrieve_contexts
Retrieves relavant contexts from the specified RAG Engine Corpus via similarity search for the given query.
Parameters * parent: The parent resource, of the form projects/{project}/locations/{location}. * query: The query to search for. * query.text: The query text to search for. * query.rag_retrieval_config: The RAG retrieval config to use for the query. * query.rag_retrieval_config.top_k: The number of contexts to retrieve. * query.rag_retrieval_config.filter: The filter config to use for the query. * query.rag_retrieval_config.filter.vector_distance_threshold: The vector distance threshold to use for the query. * vertex_rag_store: The RAG store to use for the query. * vertex_rag_store.rag_resources: a list of RAG resources to use for the query. * vertex_rag_store.rag_resources.rag_corpus: A RAG corpus to use for the query.
The following sample demonstrate how to use curl to invoke the retrieve_contexts MCP tool.
| Curl Request |
|---|
curl --location 'https://aiplatform.googleapis.com/mcp/generate' \ --header 'content-type: application/json' \ --header 'accept: application/json, text/event-stream' \ --data '{ "method": "tools/call", "params": { "name": "retrieve_contexts", "arguments": { // provide these details according to the tool's MCP specification } }, "jsonrpc": "2.0", "id": 1 }' |
Input Schema
Request message for VertexRagService.RetrieveContexts.
RetrieveContextsRequest
| JSON representation |
|---|
{ "parent": string, "query": { object ( |
| Fields | |
|---|---|
parent |
Required. The resource name of the Location from which to retrieve RagContexts. The users must have permission to make a call in the project. Format: |
query |
Required. Single RAG retrieve query. |
Union field data_source. Data Source to retrieve contexts. data_source can be only one of the following: |
|
vertexRagStore |
The data source for Vertex RagStore. |
VertexRagStore
| JSON representation |
|---|
{ "ragResources": [ { object ( |
| Fields | |
|---|---|
ragResources[] |
Optional. The representation of the rag source. It can be used to specify corpus only or ragfiles. Currently only support one corpus or multiple files from one corpus. In the future we may open up multiple corpora support. |
Union field
|
|
vectorDistanceThreshold |
Optional. Only return contexts with vector distance smaller than the threshold. |
RagResource
| JSON representation |
|---|
{ "ragCorpus": string, "ragFileIds": [ string ] } |
| Fields | |
|---|---|
ragCorpus |
Optional. RagCorpora resource name. Format: |
ragFileIds[] |
Optional. rag_file_id. The files should be in the same rag_corpus set in rag_corpus field. |
RagQuery
| JSON representation |
|---|
{ "ragRetrievalConfig": { object ( |
| Fields | |
|---|---|
ragRetrievalConfig |
Optional. The retrieval config for the query. |
Union field query. The query to retrieve contexts. Currently only text query is supported. query can be only one of the following: |
|
text |
Optional. The query in text format to get relevant contexts. |
RagRetrievalConfig
| JSON representation |
|---|
{ "topK": integer, "filter": { object ( |
| Fields | |
|---|---|
topK |
Optional. The number of contexts to retrieve. |
filter |
Optional. Config for filters. |
ranking |
Optional. Config for ranking and reranking. |
Filter
| JSON representation |
|---|
{ "metadataFilter": string, // Union field |
| Fields | |
|---|---|
metadataFilter |
Optional. String for metadata filtering. |
Union field vector_db_threshold. Filter contexts retrieved from the vector DB based on either vector distance or vector similarity. vector_db_threshold can be only one of the following: |
|
vectorDistanceThreshold |
Optional. Only returns contexts with vector distance smaller than the threshold. |
vectorSimilarityThreshold |
Optional. Only returns contexts with vector similarity larger than the threshold. |
Ranking
| JSON representation |
|---|
{ // Union field |
| Fields | |
|---|---|
Union field ranking_config. Config options for ranking. Currently only Rank Service is supported. ranking_config can be only one of the following: |
|
rankService |
Optional. Config for Rank Service. |
llmRanker |
Optional. Config for LlmRanker. |
RankService
| JSON representation |
|---|
{ // Union field |
| Fields | |
|---|---|
Union field
|
|
modelName |
Optional. The model name of the rank service. Format: |
LlmRanker
| JSON representation |
|---|
{ // Union field |
| Fields | |
|---|---|
Union field
|
|
modelName |
Optional. The model name used for ranking. See Supported models. |
Output Schema
Response message for VertexRagService.RetrieveContexts.
RetrieveContextsResponse
| JSON representation |
|---|
{
"contexts": {
object ( |
| Fields | |
|---|---|
contexts |
The contexts of the query. |
RagContexts
| JSON representation |
|---|
{
"contexts": [
{
object ( |
| Fields | |
|---|---|
contexts[] |
All its contexts. |
Context
| JSON representation |
|---|
{ "sourceUri": string, "sourceDisplayName": string, "text": string, "chunk": { object ( |
| Fields | |
|---|---|
sourceUri |
If the file is imported from Cloud Storage or Google Drive, source_uri will be original file URI in Cloud Storage or Google Drive; if file is uploaded, source_uri will be file display name. |
sourceDisplayName |
The file display name. |
text |
The text chunk. |
chunk |
Context of the retrieved chunk. |
Union field
|
|
score |
According to the underlying Vector DB and the selected metric type, the score can be either the distance or the similarity between the query and the context and its range depends on the metric type. For example, if the metric type is COSINE_DISTANCE, it represents the distance between the query and the context. The larger the distance, the less relevant the context is to the query. The range is [0, 2], while 0 means the most relevant and 2 means the least relevant. |
RagChunk
| JSON representation |
|---|
{ "text": string, // Union field |
| Fields | |
|---|---|
text |
The content of the chunk. |
Union field
|
|
pageSpan |
If populated, represents where the chunk starts and ends in the document. |
PageSpan
| JSON representation |
|---|
{ "firstPage": integer, "lastPage": integer } |
| Fields | |
|---|---|
firstPage |
Page where chunk starts in the document. Inclusive. 1-indexed. |
lastPage |
Page where chunk ends in the document. Inclusive. 1-indexed. |
Tool Annotations
Destructive Hint: ❌ | Idempotent Hint: ✅ | Read Only Hint: ✅ | Open World Hint: ❌