Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

fix(gmail): show real subjects in search results on a Composio grant (#3748)

Q1hangLc38bbdcd2608
diff --git a/src/xagent/web/tools/mcp/composio_proxy.py b/src/xagent/web/tools/mcp/composio_proxy.py
index c8a5053ae..c588231a0 100644
--- a/src/xagent/web/tools/mcp/composio_proxy.py
+++ b/src/xagent/web/tools/mcp/composio_proxy.py
@@ -834,6 +834,31 @@ def _composio_refusal(what: str, status: int, answer: Any) -> tuple[str, str | N
return message, slug
+def _checked_query_pairs(
+ query: Mapping[str, str] | Iterable[tuple[str, str]],
+) -> list[tuple[str, str]]:
+ """The query's pairs as a list, refusing a name that appears more than
+ once.
+
+ Composio's proxy appears to keep one value of a repeated query parameter,
+ so a repeated name would lose every value but one without any sign of it.
+ A caller that has several values for one name has to send them in
+ separate requests.
+ """
+ pairs = list(query.items() if isinstance(query, Mapping) else query)
+ seen: set[str] = set()
+ for name, _value in pairs:
+ if name in seen:
+ raise ValueError(
+ f"query parameter {name!r} appears more than once: Composio's "
+ "proxy appears to keep one value of a repeated query "
+ "parameter, so the other values would be dropped without "
+ "notice; give one value for it in each request"
+ )
+ seen.add(name)
+ return pairs
+
+
def _proxy_payload(
method: str,
endpoint: str,
@@ -843,7 +868,7 @@ def _proxy_payload(
body: Any = None,
) -> dict[str, Any]:
"""The JSON body one Proxy Execute call would carry, without sending it."""
- pairs = query.items() if isinstance(query, Mapping) else query
+ pairs = _checked_query_pairs(query)
connected_account_id = _required_env(COMPOSIO_CONNECTED_ACCOUNT_ID_ENV)
user_id = _required_env(COMPOSIO_USER_ID_ENV)
@@ -904,7 +929,8 @@ def check_json_body_size(
max_body_bytes: int | None = None,
) -> None:
"""Refuse a proxy call whose JSON payload is above
- ``MAX_JSON_BODY_BYTES``, before anything is sent.
+ ``MAX_JSON_BODY_BYTES``, before anything is sent; a query name that
+ appears more than once is refused too.
``proxy_request`` performs the same check. A connector that sends several
calls in one tool invocation (one message each) calls this for each of
@@ -932,12 +958,13 @@ def proxy_request(
"""Send ``method endpoint`` through Composio as the launched grant.
``allowed_hosts`` are the hostnames an absolute ``endpoint`` may name.
- ``query`` is a mapping, or a sequence of pairs when a parameter repeats;
- ``body`` is sent as JSON. Raises ``ComposioProxyError`` when Composio does
- not relay the call, and ``ValueError`` before sending anything when the
- endpoint, a launch variable or a parameter is unusable, or when the JSON
- body would exceed ``MAX_JSON_BODY_BYTES``. ``max_body_bytes`` overrides
- that shared default, for a connector whose real limit differs.
+ ``query`` is a mapping or a sequence of pairs, and each name may appear
+ only once; ``body`` is sent as JSON. Raises ``ComposioProxyError`` when
+ Composio does not relay the call, and ``ValueError`` before sending
+ anything when the endpoint, a launch variable or a parameter is unusable,
+ when a query name appears more than once, or when the JSON body would
+ exceed ``MAX_JSON_BODY_BYTES``. ``max_body_bytes`` overrides that shared
+ default, for a connector whose real limit differs.
"""
if isinstance(allowed_hosts, str):
raise TypeError("allowed_hosts must be a collection of hostnames, not a str")
diff --git a/src/xagent/web/tools/mcp/gmail.py b/src/xagent/web/tools/mcp/gmail.py
index a5fc359b8..a251c9d9f 100644
--- a/src/xagent/web/tools/mcp/gmail.py
+++ b/src/xagent/web/tools/mcp/gmail.py
@@ -212,16 +212,28 @@ def gmail_search_messages(
Search and list Gmail messages with optional query and label filters.
Use query parameter for Gmail search syntax (e.g. 'is:unread', 'from:[email protected]').
On a Composio-backed grant at most 100 messages are read in one call (the
- batch fallback sends one proxy request per message, plus the list request);
- ask for more in smaller calls.
+ batch fallback sends one proxy request per message, plus the list request),
+ so ask for more in smaller calls; pass at most one label id there, and
+ write any other condition into query (for example 'is:unread' or
+ 'label:<name>').
"""
try:
- if composio_proxy.serves_composio_grant():
+ composio_grant = composio_proxy.serves_composio_grant()
+ if composio_grant:
_check_composio_batch_budget(
max_results,
"max_results",
unit="message requests (plus the list request)",
)
+ if label_ids and len(label_ids) > 1:
+ raise ValueError(
+ "on a Composio-backed grant one search takes at most one "
+ "label id; Gmail returns only the messages that carry "
+ "every label given, so splitting the labels into separate "
+ "searches would return more than asked. Write the other "
+ "labels or conditions into query instead, for example "
+ "'label:<name>' or 'is:unread'."
+ )
service = get_gmail_service()
kwargs = {"userId": "me", "maxResults": max_results}
if query:
@@ -264,16 +276,18 @@ def gmail_search_messages(
# Utilize Google API batch requests to prevent LLM tool execution timeouts
batch = service.new_batch_http_request(callback=callback)
for msg in messages:
- batch.add(
- service.users()
- .messages()
- .get(
- userId="me",
- id=msg["id"],
- format="metadata",
- metadataHeaders=["Subject", "From"],
- )
- )
+ get_kwargs: dict[str, Any] = {
+ "userId": "me",
+ "id": msg["id"],
+ "format": "metadata",
+ }
+ # Composio's proxy appears to keep one value of a repeated query
+ # parameter, so only one of the two ``metadataHeaders``
+ # googleapiclient writes would reach Gmail. A metadata read that
+ # names no header returns every header instead.
+ if not composio_grant:
+ get_kwargs["metadataHeaders"] = ["Subject", "From"]
+ batch.add(service.users().messages().get(**get_kwargs))
_execute_batch(batch)
diff --git a/src/xagent/web/tools/mcp/google_api_proxy.py b/src/xagent/web/tools/mcp/google_api_proxy.py
index 9401636fd..e2e50307b 100644
--- a/src/xagent/web/tools/mcp/google_api_proxy.py
+++ b/src/xagent/web/tools/mcp/google_api_proxy.py
@@ -348,8 +348,8 @@ class GoogleApiProxyHttp:
body: str | bytes | None = None,
headers: Mapping[str, str] | None = None,
) -> None:
- """Refuse one request the proxy would reject for its JSON body size,
- without sending it.
+ """Refuse one request the proxy would reject for its JSON body size
+ or a query name that appears more than once, without sending it.
Sending several requests in one tool call (one message each) checks
every request first, so an oversized later one cannot leave the
@@ -371,7 +371,7 @@ class GoogleApiProxyHttp:
def precheck_body_size(request: Any) -> None:
"""Refuse one googleapiclient request the proxy would reject for its body
- size, before it is sent.
+ size or a query name that appears more than once, before it is sent.
A request on the native transport is left alone, so the native path is
unchanged.
diff --git a/tests/web/tools/test_composio_proxy.py b/tests/web/tools/test_composio_proxy.py
index 87c2392c0..883419759 100644
--- a/tests/web/tools/test_composio_proxy.py
+++ b/tests/web/tools/test_composio_proxy.py
@@ -323,6 +323,37 @@ def test_a_query_mapping_sends_each_name_and_value(proxy):
]
+def test_a_repeated_query_name_is_refused_before_sending(proxy):
+ """Composio's proxy appears to keep one value of a repeated query
+ parameter, so a name that appears twice is refused here instead of
+ losing a value without a sign of it."""
+ with pytest.raises(ValueError) as raised:
+ composio_proxy.proxy_request(
+ "GET",
+ "/services/data/v62.0/query",
+ allowed_hosts=(),
+ query=[("labelIds", "INBOX"), ("q", "is:unread"), ("labelIds", "STARRED")],
+ )
+
+ assert "labelIds" in str(raised.value)
+ assert "repeated query parameter" in str(raised.value)
+ assert proxy.calls == []
+
+
+def test_a_repeated_query_name_is_refused_by_the_size_check(proxy):
+ """``check_json_body_size`` builds the same payload ``proxy_request``
+ would, so it refuses a repeated name before anything is sent too."""
+ with pytest.raises(ValueError) as raised:
+ composio_proxy.check_json_body_size(
+ "GET",
+ "/services/data/v62.0/query",
+ query=[("labelIds", "INBOX"), ("labelIds", "STARRED")],
+ )
+
+ assert "labelIds" in str(raised.value)
+ assert proxy.calls == []
+
+
def test_allowed_hosts_given_as_one_string_is_refused(proxy):
with pytest.raises(TypeError):
composio_proxy.proxy_request(
diff --git a/tests/web/tools/test_gmail_composio.py b/tests/web/tools/test_gmail_composio.py
index 9e6558500..82e24483c 100644
--- a/tests/web/tools/test_gmail_composio.py
+++ b/tests/web/tools/test_gmail_composio.py
@@ -48,6 +48,25 @@ _NOT_FOUND = {
"errors": [{"message": "Not Found", "domain": "global"}],
}
}
+# A metadata read that names no header returns every header, in the order
+# the mail carries them; one of them is spelled differently from the name the
+# tool reads, to keep its case-insensitive lookup covered.
+_METADATA_MESSAGE = {
+ "id": "m1",
+ "threadId": "t1",
+ "snippet": "the numbers",
+ "payload": {
+ "headers": [
+ {"name": "Delivered-To", "value": "[email protected]"},
+ {"name": "Received", "value": "by mx.example.com with SMTP id abc"},
+ {"name": "DKIM-Signature", "value": "v=1; a=rsa-sha256; d=example.com"},
+ {"name": "subject", "value": "Quarterly numbers"},
+ {"name": "To", "value": "Ada <[email protected]>"},
+ {"name": "From", "value": "Grace <[email protected]>"},
+ {"name": "Date", "value": "Mon, 10 Nov 2025 09:00:00 +0000"},
+ ]
+ },
+}
_BATCH_BOUNDARY = "gmail-tools-batch"
@@ -82,7 +101,11 @@ def _batch_part(request_id: str, status: int, body: str) -> str:
class FakeNativeHttp:
- """A native grant's transport: one JSON answer, or a batch answer."""
+ """A native grant's transport: one JSON answer, or a batch answer.
+
+ ``batch_request_body`` keeps the ``multipart/mixed`` body the batch was
+ sent with, so a test can read the requests it carried.
+ """
def __init__(
self, list_body: dict[str, Any], parts: list[tuple[str, int, str]]
@@ -91,9 +114,11 @@ class FakeNativeHttp:
self._batch_body = (
"".join(_batch_part(*part) for part in parts) + f"--{_BATCH_BOUNDARY}--\r\n"
)
+ self.batch_request_body = ""
def request(self, uri, method=None, body=None, headers=None):
if "batch" in uri:
+ self.batch_request_body = body
response = httplib2.Response(
{"content-type": f'multipart/mixed; boundary="{_BATCH_BOUNDARY}"'}
)
@@ -217,6 +242,78 @@ def test_search_reads_each_message_through_the_proxy(proxy, monkeypatch):
)
+def test_search_reads_subjects_from_every_header_on_a_composio_grant(
+ proxy, monkeypatch
+):
+ """A metadata read that names no header returns every header. Asking for
+ ``metadataHeaders`` twice puts the same query name twice in one request,
+ and the proxy appears to keep one value of it (replies carried From but
+ not Subject), so every subject read "No Subject"."""
+ proxy.answer("/messages/m1", 200, _METADATA_MESSAGE)
+ proxy.answer("/messages", 200, {"messages": [{"id": "m1"}]})
+ monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
+
+ result = json.loads(gmail.gmail_search_messages("q", max_results=1))
+
+ assert result["messages"] == [
+ {
+ "id": "m1",
+ "threadId": "t1",
+ "snippet": "the numbers",
+ "subject": "Quarterly numbers",
+ "from": "Grace <[email protected]>",
+ }
+ ]
+ list_call, message_call = proxy.calls
+ assert list_call["endpoint"] == (
+ "https://gmail.googleapis.com/gmail/v1/users/me/messages"
+ )
+ query = [item for item in message_call["parameters"] if item["type"] == "query"]
+ assert {"name": "format", "value": "metadata", "type": "query"} in query
+ assert [item for item in query if item["name"] == "metadataHeaders"] == []
+
+
+def test_a_composio_search_with_two_label_ids_is_refused_before_sending(
+ proxy, monkeypatch
+):
+ """The tool refuses a second label id itself and points at ``query``: on a
+ Composio grant a search takes one label id, and Gmail returns only the
+ messages that carry every label given, so one search per label would
+ merge into a wider result. The shared client's refusal stays as a
+ backstop."""
+ proxy.answer("/messages", 200, {"messages": []})
+ monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
+
+ result = json.loads(
+ gmail.gmail_search_messages("q", label_ids=["INBOX", "STARRED"], max_results=1)
+ )
+
+ assert result["status"] == "error"
+ assert "one label id" in result["message"]
+ assert "query" in result["message"]
+ assert proxy.calls == []
+
+
+def test_a_composio_search_with_one_label_id_carries_it_once(proxy, monkeypatch):
+ """The ``gmail.search_seed_message`` smoke check searches by a single label
+ id on this path: the list request carries that one ``labelIds``
+ parameter."""
+ proxy.answer("/messages", 200, {"messages": []})
+ monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
+
+ result = json.loads(
+ gmail.gmail_search_messages(label_ids=["INBOX"], max_results=10)
+ )
+
+ assert result["status"] == "success"
+ (list_call,) = proxy.calls
+ assert list_call["endpoint"] == (
+ "https://gmail.googleapis.com/gmail/v1/users/me/messages"
+ )
+ label_ids = [item for item in list_call["parameters"] if item["name"] == "labelIds"]
+ assert label_ids == [{"name": "labelIds", "value": "INBOX", "type": "query"}]
+
+
def test_search_with_no_messages_sends_only_the_list_request(proxy, monkeypatch):
proxy.answer("/messages", 200, {"messages": []})
monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
@@ -431,20 +528,18 @@ def test_a_composio_refusal_is_reported_without_secrets(proxy, monkeypatch):
def test_the_batch_fallback_output_matches_a_native_batch(monkeypatch, proxy):
"""The same messages, one tool run through a real native batch and one
- through the proxy, come back as the same JSON."""
+ through the proxy, come back as the same messages and the same error. The
+ native run still asks for the two headers by name, so the request URI in
+ the error text differs between the two paths."""
list_body = {"messages": [{"id": "m1"}, {"id": "m2"}]}
- native_service = build(
- "gmail",
- "v1",
- http=FakeNativeHttp(
- list_body,
- [
- ("1", 200, json.dumps(_MESSAGE)),
- ("2", 404, json.dumps(_NOT_FOUND)),
- ],
- ),
- static_discovery=True,
+ native_http = FakeNativeHttp(
+ list_body,
+ [
+ ("1", 200, json.dumps(_MESSAGE)),
+ ("2", 404, json.dumps(_NOT_FOUND)),
+ ],
)
+ native_service = build("gmail", "v1", http=native_http, static_discovery=True)
for name in (
composio_proxy.COMPOSIO_API_KEY_ENV,
@@ -464,10 +559,17 @@ def test_the_batch_fallback_output_matches_a_native_batch(monkeypatch, proxy):
monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
proxied_output = json.loads(gmail.gmail_search_messages("q", max_results=2))
- assert proxied_output == native_output
- assert native_output["status"] == "success"
- assert native_output["messages"][0]["subject"] == "Hello"
+ assert proxied_output["status"] == native_output["status"] == "success"
+ assert proxied_output["messages"] == native_output["messages"]
+ assert len(proxied_output["errors"]) == len(native_output["errors"]) == 1
+ assert "HttpError 404" in proxied_output["errors"][0]
assert "HttpError 404" in native_output["errors"][0]
+ assert native_output["messages"][0]["subject"] == "Hello"
+ # A native grant still asks Gmail for the two headers by name.
+ assert (
+ "metadataHeaders=Subject&metadataHeaders=From" in native_http.batch_request_body
+ )
+ assert "metadataHeaders" not in json.dumps(proxy.calls)
def test_a_composio_gmail_grant_gets_the_composio_launch_variables(monkeypatch):
diff --git a/tests/web/tools/test_google_api_proxy.py b/tests/web/tools/test_google_api_proxy.py
index 43e7b9ed8..e7d94115f 100644
--- a/tests/web/tools/test_google_api_proxy.py
+++ b/tests/web/tools/test_google_api_proxy.py
@@ -41,7 +41,7 @@ def test_a_request_is_mapped_to_one_proxy_call(proxy):
http.request(
"https://gmail.googleapis.com/gmail/v1/users/me/messages"
- "?maxResults=10&labelIds=INBOX&labelIds=STARRED",
+ "?maxResults=10&labelIds=INBOX",
method="POST",
body='{"raw": "abc"}',
headers={"content-type": "application/json", "accept": "application/json"},
@@ -56,7 +56,6 @@ def test_a_request_is_mapped_to_one_proxy_call(proxy):
"parameters": [
{"name": "maxResults", "value": "10", "type": "query"},
{"name": "labelIds", "value": "INBOX", "type": "query"},
- {"name": "labelIds", "value": "STARRED", "type": "query"},
{"name": "content-type", "value": "application/json", "type": "header"},
{"name": "accept", "value": "application/json", "type": "header"},
],
@@ -64,36 +63,50 @@ def test_a_request_is_mapped_to_one_proxy_call(proxy):
}
+def test_a_query_name_that_repeats_in_the_uri_is_refused_before_sending(proxy):
+ """Composio's proxy appears to keep one value of a repeated query
+ parameter, so a request that would lose a value is refused here."""
+ with pytest.raises(ValueError) as raised:
+ _http().request(
+ "https://gmail.googleapis.com/gmail/v1/users/me/messages"
+ "?labelIds=INBOX&labelIds=STARRED"
+ )
+
+ assert "labelIds" in str(raised.value)
+ assert proxy.calls == []
+
+
def test_a_get_googleapiclient_rewrote_to_a_post_is_sent_as_a_get(proxy):
"""A GET whose URI is too long is sent as a POST with the query
form-encoded in the body. The proxy carries a query as parameters and a
- body only as JSON, so the query goes back on the URI."""
+ body only as JSON, so every parameter goes back on the URI."""
long_query = "x" * 2200
- _gmail_service().users().messages().list(userId="me", q=long_query).execute()
+ _gmail_service().users().messages().list(
+ userId="me", q=long_query, labelIds=["INBOX"], maxResults=5
+ ).execute()
(call,) = proxy.calls
assert call["method"] == "GET"
assert "body" not in call
assert {"name": "q", "value": long_query, "type": "query"} in call["parameters"]
+ label_ids = [item for item in call["parameters"] if item["name"] == "labelIds"]
+ assert label_ids == [{"name": "labelIds", "value": "INBOX", "type": "query"}]
+ assert {"name": "maxResults", "value": "5", "type": "query"} in call["parameters"]
assert all(item["name"] != "content-type" for item in call["parameters"])
-def test_a_long_get_keeps_its_repeated_parameters(proxy):
+def test_a_long_get_whose_query_repeats_a_name_is_refused_before_sending(proxy):
"""The form body googleapiclient moves the query of a long GET into keeps
- a repeated parameter as its pairs; restoring the GET must not collapse them
- into one."""
- _gmail_service().users().messages().list(
- userId="me", q="x" * 2200, labelIds=["INBOX", "STARRED"]
- ).execute()
-
- (call,) = proxy.calls
- assert call["method"] == "GET"
- assert "body" not in call
- assert [item for item in call["parameters"] if item["name"] == "labelIds"] == [
- {"name": "labelIds", "value": "INBOX", "type": "query"},
- {"name": "labelIds", "value": "STARRED", "type": "query"},
- ]
+ a repeated parameter as its pairs; restoring the GET must not send them,
+ because the proxy appears to keep one value of a repeated name."""
+ with pytest.raises(ValueError) as raised:
+ _gmail_service().users().messages().list(
+ userId="me", q="x" * 2200, labelIds=["INBOX", "STARRED"]
+ ).execute()
+
+ assert "labelIds" in str(raised.value)
+ assert proxy.calls == []
def test_only_headers_that_change_the_answer_are_forwarded(proxy):
@@ -308,17 +321,14 @@ def test_a_body_that_is_not_json_is_refused_before_sending(proxy, body):
def _added_batch(service: Any, ids: list[str], callback) -> Any:
+ """One metadata read per id, the shape a Composio grant asks for: no
+ ``metadataHeaders``, whose repeated name the proxy appears to collapse."""
batch = service.new_batch_http_request(callback=callback)
for message_id in ids:
batch.add(
service.users()
.messages()
- .get(
- userId="me",
- id=message_id,
- format="metadata",
- metadataHeaders=["Subject", "From"],
- )
+ .get(userId="me", id=message_id, format="metadata")
)
return batch
@@ -360,8 +370,6 @@ def test_a_batch_request_keeps_the_query_googleapiclient_built(proxy):
assert call["method"] == "GET"
assert call["parameters"] == [
{"name": "format", "value": "metadata", "type": "query"},
- {"name": "metadataHeaders", "value": "Subject", "type": "query"},
- {"name": "metadataHeaders", "value": "From", "type": "query"},
{"name": "alt", "value": "json", "type": "query"},
{"name": "accept", "value": "application/json", "type": "header"},
]