Skip to content

Conversation

@ldjebran
Copy link
Contributor

@ldjebran ldjebran commented Jul 11, 2025

Description

In some configurations it's very hard for the application to be configured using the urls of mcp servers as the urls may be generated dynamically, this implementation simplify a lot the application deployments, as mcp servers are defined in earlier stages.

This PR allow the client to provide mcp headers with mcp servers names mcp_headers = {
"mcp-server-url-1": {"HEADER-1": "HEADER-1-VALUE-1", "HEADER-1": "HEADER-1-VALUE-2"},
"mcp-server-name": {"HEADER-2": "HEADER-2-VALUE"}
}

the header with "mcp-server-name" will be replaced with it corresponding url using the configuration.mcp_servers, if it does exit it's getting ignored.

for url validation we use urllib urlpase the same used by llama-stack

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement

Related Tickets & Documents

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Summary by CodeRabbit

  • New Features

    • Improved handling of MCP headers by supporting both server names and URLs, automatically mapping server names to their configured URLs where possible.
  • Bug Fixes

    • Ensured that only valid and known MCP server URLs are included when processing headers, excluding unknown entries.
  • Tests

    • Updated tests to reflect the enhanced handling and validation of MCP headers, including scenarios with both server names and URLs.

@ldjebran ldjebran requested review from manstis, tisnik and umago July 11, 2025 06:56
@ldjebran ldjebran added the enhancement New feature or request label Jul 11, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

"""

Walkthrough

A new function, handle_mcp_headers_with_toolgroups, was introduced to process MCP headers by mapping toolgroup names to their corresponding server URLs using the application configuration. This function is now used in both the synchronous and streaming query endpoints. Associated unit tests were updated to reflect and validate this new header processing behavior.

Changes

File(s) Change Summary
src/utils/mcp_headers.py Added handle_mcp_headers_with_toolgroups function for mapping toolgroup names in MCP headers to server URLs.
src/app/endpoints/query.py, src/app/endpoints/streaming_query.py Integrated handle_mcp_headers_with_toolgroups into response retrieval, replacing direct MCP header usage.
tests/unit/app/endpoints/test_query.py, tests/unit/app/endpoints/test_streaming_query.py Updated tests to use toolgroup names in MCP headers and validate correct mapping to server URLs.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Endpoint (Query/Streaming)
    participant MCP Header Handler
    participant AppConfig

    Client->>Endpoint (Query/Streaming): Send request with mcp_headers
    Endpoint (Query/Streaming)->>MCP Header Handler: handle_mcp_headers_with_toolgroups(mcp_headers, AppConfig)
    MCP Header Handler->>AppConfig: Lookup server URLs for toolgroup names
    MCP Header Handler-->>Endpoint (Query/Streaming): Return processed mcp_headers (URLs as keys)
    Endpoint (Query/Streaming)-->>Client: Continue processing with updated mcp_headers
Loading

Poem

🐇
In the garden of headers, we hop and we map,
Toolgroups to URLs, in a clever little wrap.
Tests now align, with names and with care,
Only known servers get tokens to share.
A hop, a skip, a mapping delight—
Our headers are tidy, our code feels just right!

"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between df64e8c and 8dd0435.

📒 Files selected for processing (5)
  • src/app/endpoints/query.py (2 hunks)
  • src/app/endpoints/streaming_query.py (2 hunks)
  • src/utils/mcp_headers.py (2 hunks)
  • tests/unit/app/endpoints/test_query.py (2 hunks)
  • tests/unit/app/endpoints/test_streaming_query.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/app/endpoints/streaming_query.py
  • src/app/endpoints/query.py
  • src/utils/mcp_headers.py
  • tests/unit/app/endpoints/test_query.py
  • tests/unit/app/endpoints/test_streaming_query.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-pr
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2f71c65 and df64e8c.

📒 Files selected for processing (5)
  • src/app/endpoints/query.py (2 hunks)
  • src/app/endpoints/streaming_query.py (2 hunks)
  • src/utils/mcp_headers.py (2 hunks)
  • tests/unit/app/endpoints/test_query.py (2 hunks)
  • tests/unit/app/endpoints/test_streaming_query.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/app/endpoints/streaming_query.py (1)
src/utils/mcp_headers.py (1)
  • handle_mcp_headers_with_toolgroups (55-74)
src/app/endpoints/query.py (1)
src/utils/mcp_headers.py (1)
  • handle_mcp_headers_with_toolgroups (55-74)
src/utils/mcp_headers.py (1)
src/configuration.py (3)
  • configuration (47-52)
  • AppConfig (20-100)
  • mcp_servers (79-84)
🪛 GitHub Actions: Pydocstyle
src/utils/mcp_headers.py

[error] 55-55: pydocstyle: Missing docstring in public function handle_mcp_headers_with_toolgroups (D103)

🪛 GitHub Actions: Python linter
src/utils/mcp_headers.py

[warning] 55-55: pylint: Missing function or method docstring (missing-function-docstring)


[warning] 9-9: pylint: third party import "fastapi.Request" should be placed before first party import "configuration.AppConfig" (wrong-import-order)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-pr
🔇 Additional comments (8)
src/app/endpoints/query.py (2)

32-32: LGTM: Clean import addition.

The import is properly added and aligns with the new MCP headers processing functionality.


234-234: LGTM: Proper integration of header processing.

The function is correctly placed in the flow - after ensuring mcp_headers is not None and before using the headers for authorization setup. This ensures that toolgroup names are properly converted to URLs before being used.

src/app/endpoints/streaming_query.py (2)

25-25: LGTM: Consistent import pattern.

The import matches the pattern used in query.py, maintaining consistency across both endpoint files.


294-294: LGTM: Consistent header processing implementation.

The function integration is identical to the synchronous query endpoint, ensuring consistent behavior across both streaming and non-streaming queries. The placement in the function flow is appropriate.

tests/unit/app/endpoints/test_query.py (2)

696-705: LGTM: Comprehensive test input covering all scenarios.

The test input properly covers all the scenarios that the new function should handle:

  • Server names that map to known MCP servers
  • Valid URLs that should pass through unchanged
  • Unknown server names that should be filtered out

This provides good coverage for the new functionality.


727-734: LGTM: Correct expected output validation.

The expected headers correctly demonstrate the mapping behavior:

  • Server names converted to their corresponding URLs
  • Direct URLs passed through unchanged
  • Unknown servers filtered out as documented in the comment

This properly validates the handle_mcp_headers_with_toolgroups function behavior.

tests/unit/app/endpoints/test_streaming_query.py (2)

764-773: LGTM: Consistent test input with synchronous query tests.

The test input is identical to the synchronous query test, ensuring consistent validation across both streaming and non-streaming endpoints. The comprehensive scenario coverage is maintained.


795-802: LGTM: Consistent expected output validation.

The expected output validation matches the synchronous query test, ensuring both endpoints handle MCP headers identically. The filtering and mapping behavior is properly verified.

Copy link
Contributor

@manstis manstis left a comment

Choose a reason for hiding this comment

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

Great. LGTM 👍

I discovered make verify to run all checks locally.

https://issues.redhat.com/browse/LCORE-353
this PR allow the client to provide mcp headers with mcp servers names
mcp_headers = {
    "mcp-server-url-1": {"HEADER-1": "HEADER-1-VALUE-1", "HEADER-1": "HEADER-1-VALUE-2"},
    "mcp-server-name": {"HEADER-2": "HEADER-2-VALUE"}
}

the header with "mcp-server-name" will be replaced with it corresponding url using the configuration.mcp_servers, if it does exit it's getting ignored.

Signed-off-by: Djebran Lezzoum <ldjebran@gmail.com>
@ldjebran ldjebran force-pushed the allow-mcp-headers-with-mcp-servers-name branch from df64e8c to 8dd0435 Compare July 11, 2025 07:14
Copy link
Contributor

@tisnik tisnik left a comment

Choose a reason for hiding this comment

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

it looks ok, thank you

@tisnik tisnik merged commit 71d3667 into lightspeed-core:main Jul 11, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants