|
| 1 | +//! The MCP endpoint answers on the MCP host. rmcp refuses hosts it wasn't told about (DNS rebinding protection) with a |
| 2 | +//! 403 "Host header is not allowed", so the host `VISUALCI_MCP_URL` names must be on its list. A file of its own: the |
| 3 | +//! MCP service is built once per process, from the first test's settings. |
| 4 | +
|
| 5 | +use app::models::{Organization, User}; |
| 6 | +use laravel::db; |
| 7 | +use laravel::model::create; |
| 8 | +use laravel::testing::prelude::*; |
| 9 | +use serde_json::json; |
| 10 | + |
| 11 | +fn hosts() -> Application { |
| 12 | + app::testing::configure() |
| 13 | + .with_envs([ |
| 14 | + ("VISUALCI_SITE_URL", "https://visualci.test"), |
| 15 | + ("VISUALCI_APP_URL", "https://app.visualci.test"), |
| 16 | + ("VISUALCI_API_URL", "https://api.visualci.test"), |
| 17 | + ("VISUALCI_MCP_URL", "https://mcp.visualci.test"), |
| 18 | + ]) |
| 19 | + .create() |
| 20 | +} |
| 21 | + |
| 22 | +#[laravel::test(app = hosts)] |
| 23 | +async fn mcp_answers_on_the_mcp_host(app: Application) -> laravel::Result<()> { |
| 24 | + let email = format!("{}@example.com", laravel::str::uuid7()); |
| 25 | + let user = create!(User { pid: laravel::str::uuid7(), name: "owner", email: email, staff: false }).await?; |
| 26 | + let org = create!(Organization { pid: laravel::str::uuid7(), slug: "acme", name: "Acme" }).await?; |
| 27 | + app::testing::fixtures::member(&org, &user, "admin").await?; |
| 28 | + let token = format!("vcu_{}", laravel::str::uuid7().simple()); |
| 29 | + let (hash, short, owner) = (app::models::token_hash(&token), token[..6].to_string(), user.id); |
| 30 | + db::query!( |
| 31 | + "INSERT INTO user_tokens (user_id, name, token_hash, token_prefix, created_at, updated_at) \ |
| 32 | + VALUES ({owner}, 'agent', {hash}, {short}, now(), now())" |
| 33 | + ) |
| 34 | + .insert() |
| 35 | + .await?; |
| 36 | + |
| 37 | + let initialize = json!({ |
| 38 | + "jsonrpc": "2.0", "id": 1, "method": "initialize", |
| 39 | + "params": { "protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": { "name": "test", "version": "1" } } |
| 40 | + }); |
| 41 | + let response = app |
| 42 | + .post("/mcp") |
| 43 | + .header("host", "mcp.visualci.test") |
| 44 | + .header("authorization", format!("Bearer {token}")) |
| 45 | + // After the body: `json` sets `accept: application/json`, and MCP's Streamable HTTP wants both. |
| 46 | + .json(initialize.clone()) |
| 47 | + .header("accept", "application/json, text/event-stream") |
| 48 | + .await; |
| 49 | + response.assert_ok(); |
| 50 | + let body = response.json().clone(); |
| 51 | + assert!(body["result"]["serverInfo"].is_object(), "{body}"); |
| 52 | + |
| 53 | + // A host that isn't ours is still refused, by the host check before rmcp sees it. |
| 54 | + let response = app |
| 55 | + .post("/mcp") |
| 56 | + .header("host", "evil.example.com") |
| 57 | + .header("authorization", format!("Bearer {token}")) |
| 58 | + .json(initialize) |
| 59 | + .header("accept", "application/json, text/event-stream") |
| 60 | + .await; |
| 61 | + assert!(response.status().is_client_error(), "{}", response.status()); |
| 62 | + Ok(()) |
| 63 | +} |
0 commit comments