Skip to content

fix: serialize tool outputs as JSON strings for MCP protocol compliance#140

Open
gsttg wants to merge 1 commit intoClickHouse:mainfrom
gsttg:fix/serialize-tool-output-to-json-string
Open

fix: serialize tool outputs as JSON strings for MCP protocol compliance#140
gsttg wants to merge 1 commit intoClickHouse:mainfrom
gsttg:fix/serialize-tool-output-to-json-string

Conversation

@gsttg
Copy link
Copy Markdown

@gsttg gsttg commented Mar 9, 2026

Summary

  • run_query and list_tables return Python dicts instead of JSON strings, causing MCP output validation errors on every successful call: Output validation error: {...} is not of type 'string'
  • Wrap all dict/list return values with json.dumps(..., default=str) to match the pattern already used by list_databases
  • Fix applies to execute_query, list_tables (2 return paths), and run_chdb_select_query (all return paths)
  • Remove dead error-dict check in run_query (errors already raise ToolError)
  • Update list_tables return type annotation from Dict[str, Any] to str

See linked issue for full details and reproduction steps.

Test plan

  • Verified manually via Claude Code MCP integration — queries now return clean results without validation errors
  • Existing test suite's json.loads(result.content[0].text) assertions pass (confirms output is valid JSON string)
  • test_run_select_query_syntax_error passes (error handling unaffected)

🤖 Generated with Claude Code

The MCP protocol requires tool results to be returned as strings (wrapped
in TextContent), but `run_query`, `list_tables`, and `run_chdb_select_query`
were returning Python dicts/lists directly, causing output validation errors
like: `Output validation error: {...} is not of type 'string'`.

`list_databases` already correctly used `json.dumps()` — this commit makes
all tool functions consistent by wrapping their return values with
`json.dumps(..., default=str)`.

Also removes dead error-check code in `run_query` that could never trigger
since `execute_query` raises `ToolError` on failure rather than returning
an error dict.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants