@devries-cameron20 / composio-1 / commits / 4367513f8
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(python): guard enhance_schema_descriptions against empty schemas (#3398)

Stacked on #3397 · refs https://github.com/ComposioHQ/composio/issues/3354 ## Summary The TS fix in #3397 handled the reported case (`output_parameters: {}` from MCP toolkits) by normalizing empty schemas at `transformToolCases`. While verifying the Python SDK didn't have the equivalent Zod check, I found an **adjacent** crash on the input side. The Python SDK does not have the same Pydantic check** — `composio_client` types `input_parameters` / `output_parameters` as `Dict[str, Optional[object]]`, which accepts `{}` trivially. However, `Tools._get()` pipes every fetched tool's `input_parameters` unconditionally through `FileHelper.enhance_schema_descriptions` (`python/composio/core/models/tools.py:361-363`), which does: ```python for _param, _schema in schema["properties"].items(): ``` That crashes with `KeyError: 'properties'` on `schema={}`. Not reachable for the specific `granola_mcp` tools in the original report (they all have populated `input_parameters`), but it's the same API contract — any MCP tool with no required inputs would hit this through the public `Composio.tools.get(...)` call. The sibling `FileHelper.process_file_uploadable_schema` (`_files.py:760`) already guards this with `if "properties" not in schema: return schema`. This applies the same guard to `enhance_schema_descriptions`. ## Fix ```diff def enhance_schema_descriptions(self, schema: t.Dict) -> t.Dict: ... + if "properties" not in schema: + return schema required = schema.get("required") or [] for _param, _schema in schema["properties"].items(): ``` ## Testing Red → green confirmed locally. New `TestEnhanceSchemaDescriptionsEmptySchema` class in `tests/test_files.py` covers: 1. `schema={}` → returns `{}` (the bug repro). 2. Schema with metadata but no `properties` key → returned unchanged (mirrors the sibling method). 3. Schema with `properties: {}` (empty dict) → already works today; pinning behavior. 4. Populated schema → still enhanced (regression guard for the type-hint / required-marker enhancement). Cases 1–2 **fail on `next`** with `KeyError: 'properties'`. All 4 pass with the guard. Full `tests/test_files.py` stays green (116 tests). ```bash cd python .venv/bin/python -m pytest tests/test_files.py::TestEnhanceSchemaDescriptionsEmptySchema -v # → 4 passed .venv/bin/python -m pytest tests/test_files.py -q # → 116 passed ``` ## Stacked PR notes - Base: `jkomyno/fix-mcp-empty-output-parameters` (#3397). - This PR's diff against that base shows **only** the Python changes (`_files.py` + `tests/test_files.py`). - After #3397 merges, this should be retargeted to `next` (or rebased automatically by GitHub). --------- Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>

Alberto Schiabel committed May 16, 2026 at 23:03 UTC 4367513f8233be30759c328a5bbe7b8405d1553a
2 files changed +72
python/composio/core/models/_files.py
+6
@@ -739,7 +739,13 @@ class FileHelper(WithLogger):
739
740 This is separate from file processing and should always run
741 regardless of `dangerously_allow_auto_upload_download_files`.
742 +
743 + Schemas with no `properties` key (e.g. `{}` for tools that declare
744 + no input parameters — common with MCP-backed toolkits) are returned
745 + unchanged, matching the sibling `process_file_uploadable_schema`.
746 """
747 + if "properties" not in schema:
748 + return schema
749 required = schema.get("required") or []
750 for _param, _schema in schema["properties"].items():
751 if _schema.get("type") in ["string", "integer", "number", "boolean"]:
python/tests/test_files.py
+66
@@ -2406,3 +2406,69 @@ class TestFileDownloadablePathTraversal:
2406 # but no file was written inside it.
2407 assert outdir.is_dir()
2408 assert list(outdir.iterdir()) == []
2409 +
2410 +
2411 +class TestEnhanceSchemaDescriptionsEmptySchema:
2412 + """Regression tests for the empty-input-parameters crash.
2413 +
2414 + MCP-backed toolkits can return `input_parameters: {}` for tools with no
2415 + required arguments (the same convention the API uses for missing output
2416 + schemas). Before the fix, `FileHelper.enhance_schema_descriptions` —
2417 + called unconditionally on every fetched tool's input_parameters from
2418 + `Tools._get()` — raised `KeyError: 'properties'` on that shape,
2419 + crashing the public `Composio.tools.get(...)` call.
2420 +
2421 + The sibling `FileHelper.process_file_uploadable_schema` already guarded
2422 + this with `if "properties" not in schema: return schema`. This adds the
2423 + same guard to `enhance_schema_descriptions`.
2424 +
2425 + See https://github.com/ComposioHQ/composio/issues/3354 for the related
2426 + TS-side issue.
2427 + """
2428 +
2429 + def test_empty_schema_does_not_raise(self, file_helper):
2430 + """schema={} must not raise — it means 'no declared schema'."""
2431 + # Pre-fix: KeyError: 'properties'
2432 + result = file_helper.enhance_schema_descriptions(schema={})
2433 + assert result == {}
2434 +
2435 + def test_schema_without_properties_returns_unchanged(self, file_helper):
2436 + """A schema with metadata but no `properties` key is returned as-is.
2437 +
2438 + Mirrors how `process_file_uploadable_schema` treats the same input.
2439 + """
2440 + schema = {"type": "object", "additionalProperties": False}
2441 + result = file_helper.enhance_schema_descriptions(schema=schema)
2442 + assert result == {"type": "object", "additionalProperties": False}
2443 +
2444 + def test_schema_with_empty_properties_dict_is_handled(self, file_helper):
2445 + """`properties: {}` is valid JSON Schema (declares no fields).
2446 +
2447 + The loop iterates zero times and the schema is returned unchanged.
2448 + """
2449 + schema = {"type": "object", "properties": {}}
2450 + result = file_helper.enhance_schema_descriptions(schema=schema)
2451 + assert result == {"type": "object", "properties": {}}
2452 +
2453 + def test_populated_schema_still_enhanced(self, file_helper):
2454 + """Non-empty schemas continue to receive type-hint enhancements.
2455 +
2456 + Guard against regressing the enhancement behavior while fixing
2457 + the empty-schema crash.
2458 + """
2459 + schema = {
2460 + "type": "object",
2461 + "properties": {
2462 + "query": {"type": "string", "description": "Search term"},
2463 + },
2464 + "required": ["query"],
2465 + }
2466 + result = file_helper.enhance_schema_descriptions(schema=schema)
2467 + description = result["properties"]["query"]["description"]
2468 + # The description must have been mutated from the input and must
2469 + # mention the type and the required-marker. Assertions are token-
2470 + # based to avoid coupling to the exact phrasing in
2471 + # enhance_schema_descriptions.
2472 + assert description != "Search term"
2473 + assert "string" in description
2474 + assert "required" in description