Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ic-bn-lib = { version = "0.8.1", features = [
] }
ic-http-certification = { version = "3.1.0", optional = true }
ic-http-gateway-protocol = { package = "ic-http-gateway-protocol", git = "https://github.com/dfinity/ic-http-gateway-protocol", tag = "v0.6.0" }
imcp2 = { version = "0.2", optional = true }
imcp2 = { version = "0.3", optional = true }
isbot = "0.1"
itertools = "0.15.0"
lazy_static = "1.5.0"
Expand Down
36 changes: 32 additions & 4 deletions src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
fmt::Display,
str::FromStr,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
time::{Duration, SystemTime, UNIX_EPOCH},
};

use anyhow::{Context, Error, anyhow};
Expand All @@ -14,6 +14,7 @@ use axum::{
response::{IntoResponse, Redirect, Response},
};
use candid::Principal;
use http::Uri;
use ic_bn_lib::{
http::extract_authority,
tasks::{Run, TaskManager},
Expand Down Expand Up @@ -89,6 +90,16 @@ impl Run for McpWrapper {
}
}

struct MetricsWrapper(Metrics);

#[async_trait]
impl Run for MetricsWrapper {
async fn run(&self, _token: CancellationToken) -> Result<(), Error> {
self.0.refresh().await;
Ok(())
}
}

pub struct McpState {
hostname: String,
router: Router,
Expand Down Expand Up @@ -156,7 +167,11 @@ pub fn setup_mcp(
)
.context("unable to create MCP metrics")?;

let mcp_redirect_url = cli.mcp_root_redirect.to_string();
let mcp_redirect_url = cli
.mcp_root_redirect
.to_string()
.trim_end_matches('/')
.to_string();
let hostname = cli
.mcp_public_url
.as_ref()
Expand All @@ -170,10 +185,23 @@ pub fn setup_mcp(
.merge(mcp.well_known_router())
.merge(mcp.root_well_known_router())
.merge(auth_callbacks_router(&[&mcp]))
.fallback(|| async move { Redirect::permanent(&mcp_redirect_url) })
.layer(from_fn_with_state(metrics, write_request_metrics));
.fallback(|uri: Uri| async move {
// Preserve path & query of the original request
let redirect_url = uri.query().map_or_else(
|| format!("{}{}", mcp_redirect_url, uri.path()),
|query| format!("{}{}?{query}", mcp_redirect_url, uri.path()),
);

Redirect::permanent(&redirect_url)
})
.layer(from_fn_with_state(metrics.clone(), write_request_metrics));

tasks.add("mcp", Arc::new(McpWrapper(mcp)));
tasks.add_interval(
"mcp-metrics-refresh",
Arc::new(MetricsWrapper(metrics)),
Duration::from_secs(5),
);

Ok(McpState { hostname, router })
}
Expand Down
Loading
Loading