8000 return json strings from query results instead of dicts by knQzx · Pull Request #154 · ClickHouse/mcp-clickhouse · GitHub
[go: up one dir, main page]

Skip to content

return json strings from query results instead of dicts - #154

Merged
joe-clickhouse merged 10 commits into
ClickHouse:mainfrom
knQzx:fix/return-json-string-from-queries
Apr 23, 2026
Merged

joe-clickhouse merged 10 commits into
ClickHouse:mainfrom
knQzx:fix/return-json-string-from-queries

Conversation

@knQzx
@knQzx knQzx commented Mar 26, 2026 •
Copy link
Copy Markdown
Contributor

list_tables, execute_query, and run_chdb_select_query return dicts/lists but the MCP protocol expects strings, so every successful call triggers a validation error. Wrap all tool responses (including the already-correct list_databases) in json.dumps(..., default=str). The default=str handles non-serializable types ClickHouse returns (e.g. datetime, UUID, Decimal, etc.). Also drop an unreachable error-dict branch in run_query and the unused to_json helper.

fixes #141

mcp protocol requires tool results to be strings but execute_query
and run_query were returning raw dicts, causing validation errors
on every successful response. also fixes large UInt64 values getting
corrupted since json.dumps with default=str serializes them properly

fixes ClickHouse#141, helps with ClickHouse#111
@CLAassistant
CLAassistant commented Mar 26, 2026 •
Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make MCP tool outputs conform to the MCP protocol’s expectation of string (TextContent) results by JSON-encoding query outputs to avoid output validation errors.

Changes:

  • JSON-encode execute_query successful results via json.dumps(..., default=str).
  • JSON-encode the (currently unreachable) error-structure return path in run_query.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mcp_clickhouse/mcp_server.py Resolved
Comment thread mcp_clickhouse/mcp_server.py Resolved
Comment thread mcp_clickhouse/mcp_server.py Resolved
knQzx added 2 commits April 20, 2026 16:04
execute_query now returns a json string on success and raises ToolError
on failure, so the dict error check never matched. the branch and its
comment are misleading, drop them
@knQzx
knQzx commented Apr 20, 2026
Copy link
Copy Markdown
Contributor Author

thanks for the review

removed the dead `isinstance(result, dict)` branch and the misleading comment about structured responses in 92a8eda

for list_tables and run_chdb_select_query returning dicts - same root issue but feels beyond this PR's scope since it touches other tools and their tests. happy to follow up in a separate PR if maintainers want that

fair catch on default=str, you're right it doesn't help UInt64 precision since python ints are already json-serializable. output_format_json_quote_64bit_integers=1 would be the proper fix but it changes the wire format for every int column, so i'd prefer to keep it a separate PR as well

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

Hi @knQzx thanks for the work so far! A few comments from my end:

  1. Getting str responses is the right move. However, since we're doing it, I'd prefer all the response patterns follow suit instead of leaving them inconsistent. So just wrap all four tools' returns with json.dumps(..., default=str). Note we'll need to adjust the relevant assertions in test_tool.py and test_chdb_tool.py after this. In the applicable places, just wrap the result in a json.loads before the type assertions.
  2. While you're in there, please also delete the dead to_json helper in mcp_server.py as it doesn't do anything.
  3. output_format_json_quote_64bit_integers=1 only works if the query to the server uses FORMAT JSON. clickhouse-connect by default will use FORMAT Native. So in order to deal with the large int problem we'd need to crawl result_rows and stringify any int outside JS safe range before json.dumps. I agree this is work for a separate PR. To pre-empt this work we could remove the json.dumps stuff to a helper that we later build in the safe js int crawler, but it'd be trvial to do when we get to the PR anyway.

@knQzx
knQzx commented Apr 23, 2026
Copy link
Copy Markdown
Contributor Author

@joe-clickhouse addressed your points

  • all four tools go through a single _serialize_tool_result helper returning json.dumps(..., default=str)
  • dropped the dead to_json
  • merged main to resolve conflicts, tests pass locally

ready for another look

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

Looks good! I think we're pretty much there. The only thing I forgot to ask is that you update the CHANGELOG as well, please. Thanks!

@knQzx
knQzx commented Apr 23, 2026
Copy link
Copy Markdown
Contributor Author

Looks good! I think we're pretty much there. The only thing I forgot to ask is that you update the CHANGELOG as well, please. Thanks!

omg, yes, did it!

@joe-clickhouse joe-clickhouse left a comment
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

@joe-clickhouse
joe-clickhouse merged commit 6e04286 into ClickHouse:main Apr 23, 2026
2 checks passed
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.

run_query and list_tables return dicts instead of JSON strings, causing MCP output validation errors

4 participants

0