-
Notifications
You must be signed in to change notification settings - Fork 45
Add support for forwarding headers from MCP clients to GraphQL APIs #428
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
Open
DaleSeo
wants to merge
4
commits into
develop
Choose a base branch
from
AIR-44
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### Add support for forwarding headers from MCP clients to GraphQL APIs - @DaleSeo PR #428 | ||
|
||
Adds opt-in support for dynamic header forwarding, which enables metadata for A/B testing, feature flagging, geo information from CDNs, or internal instrumentation to be sent from MCP clients to downstream GraphQL APIs. It automatically blocks hop-by-hop headers according to the guidelines in [RFC 7230, section 6.1](https://datatracker.ietf.org/doc/html/rfc7230#section-6.1), and it only works with the Streamable HTTP transport. | ||
|
||
You can configure using the `forward_headers` setting: | ||
|
||
```yaml | ||
forward_headers: | ||
- x-tenant-id | ||
- x-experiment-id | ||
- x-geo-country | ||
``` | ||
|
||
Please note that this feature is not intended for passing through credentials as documented in the best practices page. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,296 @@ | ||
use std::ops::Deref; | ||
use std::str::FromStr; | ||
|
||
use headers::HeaderMapExt; | ||
use http::Extensions; | ||
use reqwest::header::{HeaderMap, HeaderName}; | ||
|
||
use crate::auth::ValidToken; | ||
|
||
/// List of header names to forward from MCP clients to GraphQL API | ||
pub type ForwardHeaders = Vec<String>; | ||
|
||
/// Build headers for a GraphQL request by combining static headers with forwarded headers | ||
pub fn build_request_headers( | ||
static_headers: &HeaderMap, | ||
forward_header_names: &ForwardHeaders, | ||
incoming_headers: &HeaderMap, | ||
extensions: &Extensions, | ||
disable_auth_token_passthrough: bool, | ||
) -> HeaderMap { | ||
// Starts with static headers | ||
let mut headers = static_headers.clone(); | ||
|
||
// Forward headers dynamically | ||
forward_headers(forward_header_names, incoming_headers, &mut headers); | ||
|
||
// Optionally extract the validated token and propagate it to upstream servers if present | ||
if !disable_auth_token_passthrough && let Some(token) = extensions.get::<ValidToken>() { | ||
headers.typed_insert(token.deref().clone()); | ||
} | ||
|
||
// Forward the mcp-session-id header if present | ||
if let Some(session_id) = incoming_headers.get("mcp-session-id") { | ||
headers.insert("mcp-session-id", session_id.clone()); | ||
} | ||
|
||
headers | ||
} | ||
|
||
/// Forward matching headers from incoming headers to outgoing headers | ||
fn forward_headers(names: &[String], incoming: &HeaderMap, outgoing: &mut HeaderMap) { | ||
for header in names { | ||
if let Ok(header_name) = HeaderName::from_str(header) | ||
&& let Some(value) = incoming.get(&header_name) | ||
// Hop-by-hop headers are blocked per RFC 7230: https://datatracker.ietf.org/doc/html/rfc7230#section-6.1 | ||
&& !matches!( | ||
header_name.as_str().to_lowercase().as_str(), | ||
"connection" | ||
| "keep-alive" | ||
| "proxy-authenticate" | ||
| "proxy-authorization" | ||
| "te" | ||
| "trailers" | ||
| "transfer-encoding" | ||
| "upgrade" | ||
| "content-length" | ||
) | ||
{ | ||
outgoing.insert(header_name, value.clone()); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use headers::Authorization; | ||
use http::Extensions; | ||
use reqwest::header::HeaderValue; | ||
|
||
use crate::auth::ValidToken; | ||
|
||
#[test] | ||
fn test_build_request_headers_includes_static_headers() { | ||
let mut static_headers = HeaderMap::new(); | ||
static_headers.insert("x-api-key", HeaderValue::from_static("static-key")); | ||
static_headers.insert("user-agent", HeaderValue::from_static("mcp-server")); | ||
|
||
let forward_header_names = vec![]; | ||
let incoming_headers = HeaderMap::new(); | ||
let extensions = Extensions::new(); | ||
|
||
let result = build_request_headers( | ||
&static_headers, | ||
&forward_header_names, | ||
&incoming_headers, | ||
&extensions, | ||
false, | ||
); | ||
|
||
assert_eq!(result.get("x-api-key").unwrap(), "static-key"); | ||
assert_eq!(result.get("user-agent").unwrap(), "mcp-server"); | ||
} | ||
|
||
#[test] | ||
fn test_build_request_headers_forwards_configured_headers() { | ||
let static_headers = HeaderMap::new(); | ||
let forward_header_names = vec!["x-tenant-id".to_string(), "x-trace-id".to_string()]; | ||
|
||
let mut incoming_headers = HeaderMap::new(); | ||
incoming_headers.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); | ||
incoming_headers.insert("x-trace-id", HeaderValue::from_static("trace-456")); | ||
incoming_headers.insert("other-header", HeaderValue::from_static("ignored")); | ||
|
||
let extensions = Extensions::new(); | ||
|
||
let result = build_request_headers( | ||
&static_headers, | ||
&forward_header_names, | ||
&incoming_headers, | ||
&extensions, | ||
false, | ||
); | ||
|
||
assert_eq!(result.get("x-tenant-id").unwrap(), "tenant-123"); | ||
assert_eq!(result.get("x-trace-id").unwrap(), "trace-456"); | ||
assert!(result.get("other-header").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_build_request_headers_adds_oauth_token_when_enabled() { | ||
let static_headers = HeaderMap::new(); | ||
let forward_header_names = vec![]; | ||
let incoming_headers = HeaderMap::new(); | ||
|
||
let mut extensions = Extensions::new(); | ||
let token = ValidToken(Authorization::bearer("test-token").unwrap()); | ||
extensions.insert(token); | ||
|
||
let result = build_request_headers( | ||
&static_headers, | ||
&forward_header_names, | ||
&incoming_headers, | ||
&extensions, | ||
false, | ||
); | ||
|
||
assert!(result.get("authorization").is_some()); | ||
assert_eq!(result.get("authorization").unwrap(), "Bearer test-token"); | ||
} | ||
|
||
#[test] | ||
fn test_build_request_headers_skips_oauth_token_when_disabled() { | ||
let static_headers = HeaderMap::new(); | ||
let forward_header_names = vec![]; | ||
let incoming_headers = HeaderMap::new(); | ||
|
||
let mut extensions = Extensions::new(); | ||
let token = ValidToken(Authorization::bearer("test-token").unwrap()); | ||
extensions.insert(token); | ||
|
||
let result = build_request_headers( | ||
&static_headers, | ||
&forward_header_names, | ||
&incoming_headers, | ||
&extensions, | ||
true, | ||
); | ||
|
||
assert!(result.get("authorization").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_build_request_headers_forwards_mcp_session_id() { | ||
let static_headers = HeaderMap::new(); | ||
let forward_header_names = vec![]; | ||
|
||
let mut incoming_headers = HeaderMap::new(); | ||
incoming_headers.insert("mcp-session-id", HeaderValue::from_static("session-123")); | ||
|
||
let extensions = Extensions::new(); | ||
|
||
let result = build_request_headers( | ||
&static_headers, | ||
&forward_header_names, | ||
&incoming_headers, | ||
&extensions, | ||
false, | ||
); | ||
|
||
assert_eq!(result.get("mcp-session-id").unwrap(), "session-123"); | ||
} | ||
|
||
#[test] | ||
fn test_build_request_headers_combined_scenario() { | ||
// Static headers | ||
let mut static_headers = HeaderMap::new(); | ||
static_headers.insert("x-api-key", HeaderValue::from_static("static-key")); | ||
|
||
// Forward specific headers | ||
let forward_header_names = vec!["x-tenant-id".to_string()]; | ||
|
||
// Incoming headers | ||
let mut incoming_headers = HeaderMap::new(); | ||
incoming_headers.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); | ||
incoming_headers.insert("mcp-session-id", HeaderValue::from_static("session-456")); | ||
incoming_headers.insert( | ||
"ignored-header", | ||
HeaderValue::from_static("should-not-appear"), | ||
); | ||
|
||
// OAuth token | ||
let mut extensions = Extensions::new(); | ||
let token = ValidToken(Authorization::bearer("oauth-token").unwrap()); | ||
extensions.insert(token); | ||
|
||
let result = build_request_headers( | ||
&static_headers, | ||
&forward_header_names, | ||
&incoming_headers, | ||
&extensions, | ||
false, | ||
); | ||
|
||
// Verify all parts combined correctly | ||
assert_eq!(result.get("x-api-key").unwrap(), "static-key"); | ||
assert_eq!(result.get("x-tenant-id").unwrap(), "tenant-123"); | ||
assert_eq!(result.get("mcp-session-id").unwrap(), "session-456"); | ||
assert_eq!(result.get("authorization").unwrap(), "Bearer oauth-token"); | ||
assert!(result.get("ignored-header").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_forward_headers_no_headers_by_default() { | ||
let names: Vec<String> = vec![]; | ||
|
||
let mut incoming = HeaderMap::new(); | ||
incoming.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); | ||
|
||
let mut outgoing = HeaderMap::new(); | ||
|
||
forward_headers(&names, &incoming, &mut outgoing); | ||
|
||
assert!(outgoing.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_forward_headers_only_specific_headers() { | ||
let names = vec![ | ||
"x-tenant-id".to_string(), // Multi-tenancy | ||
"x-trace-id".to_string(), // Distributed tracing | ||
"x-geo-country".to_string(), // Geo information from CDN | ||
"x-experiment-id".to_string(), // A/B testing | ||
"ai-client-name".to_string(), // Client identification | ||
]; | ||
|
||
let mut incoming = HeaderMap::new(); | ||
incoming.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); | ||
incoming.insert("x-trace-id", HeaderValue::from_static("trace-456")); | ||
incoming.insert("x-geo-country", HeaderValue::from_static("US")); | ||
incoming.insert("x-experiment-id", HeaderValue::from_static("exp-789")); | ||
incoming.insert("ai-client-name", HeaderValue::from_static("claude")); | ||
incoming.insert("other-header", HeaderValue::from_static("ignored")); | ||
|
||
let mut outgoing = HeaderMap::new(); | ||
|
||
forward_headers(&names, &incoming, &mut outgoing); | ||
|
||
assert_eq!(outgoing.get("x-tenant-id").unwrap(), "tenant-123"); | ||
assert_eq!(outgoing.get("x-trace-id").unwrap(), "trace-456"); | ||
assert_eq!(outgoing.get("x-geo-country").unwrap(), "US"); | ||
assert_eq!(outgoing.get("x-experiment-id").unwrap(), "exp-789"); | ||
assert_eq!(outgoing.get("ai-client-name").unwrap(), "claude"); | ||
|
||
assert!(outgoing.get("other-header").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_forward_headers_blocks_hop_by_hop_headers() { | ||
let names = vec!["connection".to_string(), "content-length".to_string()]; | ||
|
||
let mut incoming = HeaderMap::new(); | ||
incoming.insert("connection", HeaderValue::from_static("keep-alive")); | ||
incoming.insert("content-length", HeaderValue::from_static("1234")); | ||
|
||
let mut outgoing = HeaderMap::new(); | ||
|
||
forward_headers(&names, &incoming, &mut outgoing); | ||
|
||
assert!(outgoing.get("connection").is_none()); | ||
assert!(outgoing.get("content-length").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_forward_headers_case_insensitive_matching() { | ||
let names = vec!["X-Tenant-ID".to_string()]; | ||
|
||
let mut incoming = HeaderMap::new(); | ||
incoming.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); | ||
|
||
let mut outgoing = HeaderMap::new(); | ||
forward_headers(&names, &incoming, &mut outgoing); | ||
|
||
assert_eq!(outgoing.get("x-tenant-id").unwrap(), "tenant-123"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want any additional validation on the strings being used here? Like size limits, or excludes
mcp-session-id
,authorization
,traceparent
, andtracestate
which we tend to handle more as special cases?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question, @swcollard ! Which of them do you think we should block from forwarding? I guess forwarding
mcp-session-id
,traceparent
andtracestate
to downstream APIs could be helpful from the observability perspective? 🤔 We recommend not using this feature for passing throughauthorization
in the best practices docs, but I think we should still leave it to the user's discretion based on customer feedback.