Skip to content

fix: mcp-client should also include configurable http headers in the /sse request #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

kagezhao
Copy link
Contributor

@kagezhao kagezhao commented Apr 2, 2025

fix: mcp-client should also include configurable http headers in the /sse request

reasons

  • In the official python sdk, custom headers can also take effect on the /sse API
  • In some scenarios, /sse also requires these http header parameters, such as authentication, etc.

Summary by CodeRabbit

  • New Features
    • Enhanced streaming connections by supporting additional custom HTTP headers for improved request customization.

Copy link
Contributor

coderabbitai bot commented Apr 2, 2025

Walkthrough

The changes modify the SSE client implementation in client/sse.go. The update introduces a loop in the Start method of the SSEMCPClient struct that iterates over a map of custom HTTP headers. Each key-value pair in this map is applied to the outgoing HTTP request. Additionally, a new field headers map[string]string is added to the struct, extending the request customization capability without affecting the existing default header setup.

Changes

File Summary
client/sse.go Added a new headers map[string]string field in SSEMCPClient and introduced a loop in the Start method to apply custom HTTP headers from the headers map.

Possibly related PRs


📜 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 f5e3dd8 and e1acf02.

📒 Files selected for processing (1)
  • client/sse.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • client/sse.go

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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: 0

🔭 Outside diff range comments (1)
client/sse.go (1)

386-396: ⚠️ Potential issue

Missing custom headers in the Initialize method

The Initialize method creates an HTTP request but doesn't set the custom headers from c.headers. For consistency, the custom headers should be applied here as well.

Apply this fix:

  req.Header.Set("Content-Type", "application/json")
+ // set custom http headers
+ for k, v := range c.headers {
+   req.Header.Set(k, v)
+ }
🧹 Nitpick comments (1)
client/sse.go (1)

96-98: Consider refactoring header setting logic to reduce duplication

The header setting logic is duplicated in both the Start and sendRequest methods. Consider extracting this to a helper method to improve maintainability.

+ // setHeaders applies custom headers to an HTTP request
+ func (c *SSEMCPClient) setHeaders(req *http.Request) {
+   for k, v := range c.headers {
+     req.Header.Set(k, v)
+   }
+ }

  // In Start method
  req.Header.Set("Accept", "text/event-stream")
  req.Header.Set("Cache-Control", "no-cache")
  req.Header.Set("Connection", "keep-alive")
- for k, v := range c.headers {
-   req.Header.Set(k, v)
- }
+ c.setHeaders(req)

  // In sendRequest method
  req.Header.Set("Content-Type", "application/json")
  // set custom http headers
- for k, v := range c.headers {
-   req.Header.Set(k, v)
- }
+ c.setHeaders(req)

  // Similarly in Initialize method when fixed

Also applies to: 308-310

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 051cda5 and f5e3dd8.

📒 Files selected for processing (1)
  • client/sse.go (1 hunks)
🔇 Additional comments (3)
client/sse.go (3)

38-38: LGTM!

The new field headers map[string]string in the SSEMCPClient struct properly stores custom HTTP headers. This aligns with the PR objective to make HTTP headers configurable.


71-71: LGTM!

Good practice to initialize the headers map in the constructor. This prevents nil pointer dereference when headers are accessed later.


96-98: LGTM!

The implementation correctly adds support for custom HTTP headers in the SSE request. This fulfills the PR objective by iterating through the headers map and setting each key-value pair on the request.

@kagezhao kagezhao reopened this Apr 7, 2025
@ezynda3 ezynda3 merged commit 1b7e34c into mark3labs:main Apr 10, 2025
3 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.

2 participants