8000 Client config override support via MCP Context Session states by andrei-katkov · Pull Request #115 · ClickHouse/mcp-clickhouse · GitHub
[go: up one dir, main page]

Skip to content

Client config override support via MCP Context Session states - #115

Merged
joe-clickhouse merged 4 commits into
ClickHouse:mainfrom
andrei-katkov:session-conf-overrides
Mar 6, 2026
Merged

joe-clickhouse merged 4 commits into
ClickHouse:mainfrom
andrei-katkov:session-conf-overrides

Conversation

@andrei-katkov
Copy link
Copy Markdown
Contributor

Adds support for middleware-driven ClickHouse client configuration overrides using FastMCP's context state management. This enables per-request customization of connection parameters without modifying environment variables.

@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.

Will need some merge conflict resolution, but otherwise looks good to me! Thanks for the contribution.

@andrei-katkov
Copy link
Copy Markdown
Contributor Author

@joe-clickhouse , the conflicts are fixed now. Thank you

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

Thanks @andrei-katkov! I'll give this another review by tomorrow.

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

Tests are failing. Looks like get_context() throws a RuntimeError when called outside of a FastMCP request context and create_clickhosue_client() is called in many places without one. Think we should be able to wrap get_context() in a try/except since the override is only relevant when we do have a request context. E.g. in mcp_server.py

try:
    ctx = get_context()
    session_config_overrides = ctx.get_state("clickhouse_client_config_overrides")
    if session_config_overrides:
        logger.info(f"Applying session-specific ClickHouse client config overrides {session_config_overrides}")
        client_config.update(session_config_overrides)
except RuntimeError:
    pass  # No active context...direct calls, health checks, tests, etc.
....

@andrei-katkov
Copy link
Copy Markdown
Contributor Author

Hello, @joe-clickhouse . Thank you for you suggestion, indeed seems like wrapping this thing in try-catch is the best we can do in this situation. So, I've implemented that. Can you please check again? Thanks

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

Hi @andrei-katkov I hope you don't mind but I made a few updates.

  1. I added CLIENT_CONFIG_OVERRIDES_KEY = "clickhouse_client_config_overrides" as a constant to make it easier to access the magic string so middleware authors don't have to copy/paste or type anyhting manually. Just use the import and you'll get it right.
  2. Updated the context state lookup to use this new constant.
  3. Changed the log line to only log override keys not values. This way in the logs you'll see just ['connect_timeout', 'send_receive_timeout', ...] instead of the full dict with potentially sensitive values depending on what you're overriding. Also added a warning log if a middleware author passes something other than a dict.
  4. Reconfigured the tests to fully exercise the implemented logic.

Take a look when you have a few minutes!

@andrei-katkov
Copy link
Copy Markdown
Contributor Author

@joe-clickhouse , your changes make total sense to me. Your test code is definitely an improvement over my version, since I do not know how to use mocks in Python.
Thank you very much for all the care and attention you have put into this PR so far.

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

@andrei-katkov my pleasure! Happy to help.

@andrei-katkov
Copy link
Copy Markdown
Contributor Author

Hello, @joe-clickhouse . I don't know about your release cycle, so can I ask you for an approximate timeline when this can be merged? Thank you

@joe-clickhouse
joe-clickhouse merged commit ea5855b into ClickHouse:main Mar 6, 2026
2 checks passed
@joe-clickhouse
Copy link
Copy Markdown
Collaborator
8000

Hi @andrei-katkov! Just merged this PR. We don't really have a defined release schedule but I'll try and get one out Monday morning pacific time if that works for you.

@andrei-katkov
Copy link
Copy Markdown
Contributor Author

Hi, @joe-clickhouse , that works perfectly for me, thank you

@joe-clickhouse
Copy link
Copy Markdown
Collaborator

Hey @andrei-katkov just wanted to let you know I haven't forgot about getting a release out. We're jest trying to get a few more important things wrapped into the next one and the needed approvals.

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.

2 participants

0