-
Notifications
You must be signed in to change notification settings - Fork 8
rust(feat): gate MCP tools with account feature flags at startup #764
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b5d8c05
rust(feat): gate MCP tools with account feature flags at startup
evan-sift 8356f2f
rust(chore): keep feature-flag names out of prose docs
evan-sift 0c008bd
rust(chore): treat a variant without a value as disabled
evan-sift 071d201
chore: merge main into feature flag gating
evan-sift 7d72e2a
rust(docs): add changelog entry for feature-flag tool gating
evan-sift 471b275
rust(docs): move feature-flag changelog entry into 0.5.0
evan-sift 5561139
Merge branch 'main' into rust/mcp-feature-flag-gating
evan-sift 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
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 |
|---|---|---|
| @@ -1,6 +1,3 @@ | ||
| [features] | ||
| test-reports = ["sift_mcp/test-reports"] | ||
|
|
||
| [package] | ||
| name = "sift_cli" | ||
| version = "0.5.0" | ||
|
|
||
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
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,137 @@ | ||
| use std::{collections::HashMap, time::Duration}; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
| use serde::Deserialize; | ||
|
|
||
| const FEATURE_FLAGS_PATH: &str = "/api/v1/feature-flags/variants"; | ||
| const REQUEST_TIMEOUT: Duration = Duration::from_secs(5); | ||
|
|
||
| pub(crate) static TOOL_FEATURE_FLAGS: &[(&str, &str)] = &[ | ||
| ("list_test_reports", "test-reports"), | ||
| ("list_test_steps", "test-reports"), | ||
| ("list_test_measurements", "test-reports"), | ||
| ("count_test_steps", "test-reports"), | ||
| ("count_test_measurements", "test-reports"), | ||
| ("create_test_report", "test-reports"), | ||
| ("append_test_measurements", "test-reports"), | ||
| ]; | ||
|
|
||
| #[derive(Clone, Debug, Default, Deserialize)] | ||
| pub struct FeatureFlags { | ||
| #[serde(default)] | ||
| variants: HashMap<String, FeatureFlagVariant>, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Deserialize)] | ||
| struct FeatureFlagVariant { | ||
| #[serde(default)] | ||
| value: String, | ||
| } | ||
|
|
||
| impl FeatureFlags { | ||
| pub fn enabled(&self, flag: &str) -> bool { | ||
| self.variants | ||
| .get(flag) | ||
| .is_some_and(|variant| !variant.value.is_empty() && variant.value != "off") | ||
| } | ||
|
|
||
| pub async fn fetch(rest_uri: &str, api_key: &str) -> Result<Self> { | ||
| let endpoint = format!("{}{FEATURE_FLAGS_PATH}", rest_uri.trim_end_matches('/')); | ||
| reqwest::Client::new() | ||
| .get(endpoint) | ||
| .timeout(REQUEST_TIMEOUT) | ||
| .bearer_auth(api_key) | ||
| .send() | ||
| .await | ||
| .context("feature flag request failed")? | ||
| .error_for_status() | ||
| .context("feature flag request returned an error status")? | ||
| .json() | ||
| .await | ||
| .context("failed to parse feature flag response") | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::HashMap; | ||
|
|
||
| use super::{FeatureFlagVariant, FeatureFlags}; | ||
| use crate::client_event::start_http_server; | ||
|
|
||
| fn response(status: &str, body: &str) -> Vec<u8> { | ||
| format!( | ||
| "HTTP/1.1 {status}\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{body}", | ||
| body.len() | ||
| ) | ||
| .into_bytes() | ||
| } | ||
|
|
||
| fn flags(value: Option<&str>) -> FeatureFlags { | ||
| let variants = value.map_or_else(HashMap::new, |value| { | ||
| HashMap::from([( | ||
| "test-flag".to_string(), | ||
| FeatureFlagVariant { | ||
| value: value.to_string(), | ||
| }, | ||
| )]) | ||
| }); | ||
| FeatureFlags { variants } | ||
| } | ||
|
|
||
| #[test] | ||
| fn enabled_requires_a_non_off_variant() { | ||
| assert!(!flags(None).enabled("test-flag")); | ||
| assert!(!flags(Some("off")).enabled("test-flag")); | ||
| assert!(!flags(Some("")).enabled("test-flag")); | ||
| assert!(flags(Some("on")).enabled("test-flag")); | ||
| assert!(flags(Some("experimental")).enabled("test-flag")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserializes_feature_flag_response() { | ||
| let flags: FeatureFlags = serde_json::from_str( | ||
| r#"{"variants":{"some-flag":{"value":"on"},"other":{"value":"off"},"bare":{}}}"#, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert!(flags.enabled("some-flag")); | ||
| assert!(!flags.enabled("other")); | ||
| assert!(!flags.enabled("bare")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_response_disables_all_flags() { | ||
| let flags: FeatureFlags = serde_json::from_str("{}").unwrap(); | ||
|
|
||
| assert!(!flags.enabled("test-flag")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn fetches_feature_flags_with_the_expected_request() { | ||
| let (rest_uri, server) = start_http_server(response( | ||
| "200 OK", | ||
| r#"{"variants":{"test-reports":{"value":"on"}}}"#, | ||
| )) | ||
| .await; | ||
|
|
||
| let flags = FeatureFlags::fetch(&format!("{rest_uri}/"), "test-key") | ||
| .await | ||
| .unwrap(); | ||
| assert!(flags.enabled("test-reports")); | ||
|
|
||
| let request = String::from_utf8(server.await.unwrap()).unwrap(); | ||
| let (headers, _) = request.split_once("\r\n\r\n").unwrap(); | ||
| assert!(headers.starts_with("GET /api/v1/feature-flags/variants HTTP/1.1")); | ||
| assert!( | ||
| headers | ||
| .lines() | ||
| .any(|line| line.eq_ignore_ascii_case("authorization: Bearer test-key")) | ||
| ); | ||
|
|
||
| let (rest_uri, server) = | ||
| start_http_server(response("500 Internal Server Error", "{}")).await; | ||
| assert!(FeatureFlags::fetch(&rest_uri, "test-key").await.is_err()); | ||
| server.await.unwrap(); | ||
| } | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.