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 ( |
parent
string
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: projects/{project}/locations/{location}
.
query
object (
RagQuery
)
Required. Single RAG retrieve query.
data_source
. Data Source to retrieve contexts. data_source
can be only one of the following:vertexRagStore
object (
VertexRagStore
)
The data source for Vertex RagStore.
VertexRagStore
| JSON representation |
|---|
{ "ragResources" : [ { object ( |
ragResources[]
object (
RagResource
)
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 _vector_distance_threshold
.
_vector_distance_threshold
can be only one of the following:
vectorDistanceThreshold
(deprecated)
number
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 ( |
ragRetrievalConfig
object (
RagRetrievalConfig
)
Optional. The retrieval config for the query.
query
. The query to retrieve contexts. Currently only text query is supported. query
can be only one of the following:text
string
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 |
metadataFilter
string
Optional. String for metadata filtering.
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
number
Optional. Only returns contexts with vector distance smaller than the threshold.
vectorSimilarityThreshold
number
Optional. Only returns contexts with vector similarity larger than the threshold.
Ranking
| JSON representation |
|---|
{ // Union field |
ranking_config
. Config options for ranking. Currently only Rank Service is supported. ranking_config
can be only one of the following:rankService
object (
RankService
)
Optional. Config for Rank Service.
llmRanker
object (
LlmRanker
)
Optional. Config for LlmRanker.
RankService
| JSON representation |
|---|
{ // Union field |
Union field _model_name
.
_model_name
can be only one of the following:
modelName
string
Optional. The model name of the rank service. Format: semantic-ranker-512@latest
LlmRanker
| JSON representation |
|---|
{ // Union field |
Union field _model_name
.
_model_name
can be only one of the following:
modelName
string
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 ( |
sourceUri
string
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
string
The file display name.
text
string
The text chunk.
chunk
object (
RagChunk
)
Context of the retrieved chunk.
Union field _score
.
_score
can be only one of the following:
score
number
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 |
text
string
The content of the chunk.
Union field _page_span
.
_page_span
can be only one of the following:
pageSpan
object (
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: ❌

