//! Request identity end to end: published tool call, preservation/generation of the request id and //! the MCP ingress log projection, including this group's only-consumer log writer fixture. //! //! Tests moved verbatim from `transport_protocol.rs` (Story 3.6, kanban t_987bf309); only the module //! prefix changed. use super::super::common::*; use axum::{ Json, Router, http::header, response::sse::{Event, KeepAlive, Sse}, routing::{get, post}, }; use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD}; use crank_core::{ Agent, AgentId, AgentOperationBinding, AgentStatus, AgentVersion, ExecutionConfig, HttpMethod, Operation, OperationId, OperationStatus, PlatformApiKey, PlatformApiKeyId, PlatformApiKeyScope, PlatformApiKeyStatus, Protocol, RestTarget, Target, ToolAccessMode, ToolDescription, ToolGroup, ToolSearchSettings, ToolSelectionPolicy, WorkspaceId, }; use crank_observability::{ ObservabilityConfig, RedactionLimits, ServiceIdentity, build_subscriber, }; use crank_registry::{ CreateAgentRequest, CreatePlatformApiKeyRequest, ListInvocationLogsQuery, PostgresRegistry, PublishAgentRequest, PublishRequest, SaveAgentBindingsRequest, }; use serde_json::{Value, json}; use std::{ collections::BTreeMap, io, sync::{Arc, Mutex}, time::Duration, }; use time::{OffsetDateTime, format_description::well_known::Rfc3339}; use tracing_subscriber::fmt::MakeWriter; use uuid::Version; #[derive(Clone, Default)] struct SharedLogWriter { buffer: Arc>>, } impl SharedLogWriter { fn output(&self) -> String { String::from_utf8(self.buffer.lock().unwrap().clone()).unwrap() } } impl<'a> MakeWriter<'a> for SharedLogWriter { type Writer = SharedLogGuard; fn make_writer(&'a self) -> Self::Writer { SharedLogGuard { buffer: Arc::clone(&self.buffer), } } } struct SharedLogGuard { buffer: Arc>>, } impl io::Write for SharedLogGuard { fn write(&mut self, bytes: &[u8]) -> io::Result { self.buffer.lock().unwrap().extend_from_slice(bytes); Ok(bytes.len()) } fn flush(&mut self) -> io::Result<()> { Ok(()) } } #[tokio::test] async fn initializes_lists_and_calls_published_tool_via_mcp() { let registry = test_registry().await; let upstream_base_url = spawn_upstream_server().await; let operation = test_operation(&upstream_base_url, "crm_create_lead"); registry .create_operation(&test_workspace_id(), &operation, Some("alice")) .await .unwrap(); registry .publish_operation(PublishRequest { workspace_id: &test_workspace_id(), operation_id: &operation.id, version: 1, published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(), published_by: Some("alice"), }) .await .unwrap(); publish_agent_for_operation(®istry, &operation, "sales-rest").await; let api_key = create_platform_api_key( ®istry, "sales-rest", "mcp-rest", &[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], ) .await; let base_url = spawn_mcp_server(build_test_app( registry.clone(), Duration::from_millis(0), Some("https://crank.example.com".to_owned()), )) .await; let client = reqwest::Client::new(); let mcp_url = agent_mcp_url(&base_url, "sales-rest"); let initialized_session = initialize_session(&client, &mcp_url, &api_key).await; let tools = post_jsonrpc( &client, &mcp_url, &api_key, Some(&initialized_session), json!({ "jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {} }), ) .await; let call_result = post_jsonrpc( &client, &mcp_url, &api_key, Some(&initialized_session), json!({ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "crm_create_lead", "arguments": { "email": "user@example.com" } } }), ) .await; assert_eq!(tools["result"]["tools"][0]["name"], "crm_create_lead"); assert_eq!( call_result["result"]["structuredContent"], json!({ "id": "lead_123" }) ); assert_eq!(call_result["result"]["isError"], false); let logs = registry .list_invocation_logs(ListInvocationLogsQuery { workspace_id: &test_workspace_id(), level: None, status: None, outcome_group: None, search_text: None, source: Some(crank_core::InvocationSource::AgentToolCall), operation_id: Some(&operation.id), agent_id: None, created_after: None, created_before: None, cursor_created_at: None, cursor_id: None, limit: 10, }) .await .unwrap(); assert_eq!(logs.len(), 1); assert_eq!( logs[0].log.source, crank_core::InvocationSource::AgentToolCall ); assert_eq!(logs[0].log.status, crank_core::InvocationStatus::Ok); assert_eq!(logs[0].log.tool_name, "crm_create_lead"); let keys = registry .list_platform_api_keys(&test_workspace_id()) .await .unwrap(); assert!(keys[0].api_key.last_used_at.is_some()); } #[tokio::test] async fn preserves_request_id_for_tool_call_invocations() { let registry = test_registry().await; let upstream_base_url = spawn_upstream_server().await; let operation = test_operation(&upstream_base_url, "crm_request_id"); registry .create_operation(&test_workspace_id(), &operation, Some("alice")) .await .unwrap(); registry .publish_operation(PublishRequest { workspace_id: &test_workspace_id(), operation_id: &operation.id, version: 1, published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(), published_by: Some("alice"), }) .await .unwrap(); publish_agent_for_operation(®istry, &operation, "sales-request-id").await; let api_key = create_platform_api_key( ®istry, "sales-request-id", "mcp-request-id", &[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], ) .await; let base_url = spawn_mcp_server(build_test_app( registry.clone(), Duration::from_millis(0), Some("https://crank.example.com".to_owned()), )) .await; let client = reqwest::Client::new(); let mcp_url = agent_mcp_url(&base_url, "sales-request-id"); let initialized_session = initialize_session(&client, &mcp_url, &api_key).await; let response = post_jsonrpc_response( &client, &mcp_url, &api_key, Some(&initialized_session), Some("req_test_123"), json!({ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "crm_request_id", "arguments": { "email": "user@example.com" } } }), ) .await; assert_eq!( response.headers()["x-request-id"].to_str().unwrap(), "req_test_123" ); let trace_id = response.headers()["x-trace-id"] .to_str() .unwrap() .to_owned(); let call_result = response.json::().await.unwrap(); assert_eq!(call_result["result"]["isError"], false); let logs = registry .list_invocation_logs(ListInvocationLogsQuery { workspace_id: &test_workspace_id(), level: None, status: None, outcome_group: None, search_text: None, source: Some(crank_core::InvocationSource::AgentToolCall), operation_id: Some(&operation.id), agent_id: None, created_after: None, created_before: None, cursor_created_at: None, cursor_id: None, limit: 10, }) .await .unwrap(); assert_eq!(logs.len(), 1); assert_eq!(logs[0].log.request_id.as_deref(), Some("req_test_123")); assert_eq!(logs[0].log.trace_id.as_deref(), Some(trace_id.as_str())); } #[tokio::test] async fn generates_request_id_for_tool_call_responses_and_logs() { let registry = test_registry().await; let upstream_base_url = spawn_upstream_server().await; let operation = test_operation(&upstream_base_url, "crm_generated_request_id"); registry .create_operation(&test_workspace_id(), &operation, Some("alice")) .await .unwrap(); registry .publish_operation(PublishRequest { workspace_id: &test_workspace_id(), operation_id: &operation.id, version: 1, published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(), published_by: Some("alice"), }) .await .unwrap(); publish_agent_for_operation(®istry, &operation, "sales-generated-request-id").await; let api_key = create_platform_api_key( ®istry, "sales-generated-request-id", "mcp-generated-request-id", &[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], ) .await; let base_url = spawn_mcp_server(build_test_app( registry.clone(), Duration::from_millis(0), Some("https://crank.example.com".to_owned()), )) .await; let client = reqwest::Client::new(); let mcp_url = agent_mcp_url(&base_url, "sales-generated-request-id"); let initialized_session = initialize_session(&client, &mcp_url, &api_key).await; let response = post_jsonrpc_response( &client, &mcp_url, &api_key, Some(&initialized_session), None, json!({ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "crm_generated_request_id", "arguments": { "email": "user@example.com" } } }), ) .await; let request_id = response .headers() .get("x-request-id") .unwrap() .to_str() .unwrap() .to_owned(); let trace_id = response .headers() .get("x-trace-id") .unwrap() .to_str() .unwrap() .to_owned(); assert_eq!( uuid::Uuid::parse_str(&request_id).unwrap().get_version(), Some(Version::SortRand) ); let call_result = response.json::().await.unwrap(); assert_eq!(call_result["result"]["isError"], false); let logs = registry .list_invocation_logs(ListInvocationLogsQuery { workspace_id: &test_workspace_id(), level: None, status: None, outcome_group: None, search_text: None, source: Some(crank_core::InvocationSource::AgentToolCall), operation_id: Some(&operation.id), agent_id: None, created_after: None, created_before: None, cursor_created_at: None, cursor_id: None, limit: 10, }) .await .unwrap(); assert_eq!(logs.len(), 1); assert_eq!(logs[0].log.request_id.as_deref(), Some(request_id.as_str())); assert_eq!(logs[0].log.trace_id.as_deref(), Some(trace_id.as_str())); } #[tokio::test(flavor = "current_thread")] async fn emits_request_id_in_mcp_ingress_logs() { let registry = test_registry().await; let upstream_base_url = spawn_upstream_server().await; let operation = test_operation(&upstream_base_url, "crm_request_trace"); registry .create_operation(&test_workspace_id(), &operation, Some("alice")) .await .unwrap(); registry .publish_operation(PublishRequest { workspace_id: &test_workspace_id(), operation_id: &operation.id, version: 1, published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(), published_by: Some("alice"), }) .await .unwrap(); publish_agent_for_operation(®istry, &operation, "sales-request-trace").await; let api_key = create_platform_api_key( ®istry, "sales-request-trace", "mcp-request-trace", &[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], ) .await; let writer = SharedLogWriter::default(); let subscriber = build_subscriber( ObservabilityConfig::new( ServiceIdentity::try_new("mcp-server", "test", "test").unwrap(), "info", RedactionLimits::default(), ), writer.clone(), ) .unwrap(); let dispatch = tracing::Dispatch::new(subscriber); let _guard = tracing::dispatcher::set_default(&dispatch); let base_url = spawn_mcp_server(build_test_app( registry, Duration::from_millis(0), Some("https://crank.example.com".to_owned()), )) .await; let client = reqwest::Client::new(); let mcp_url = agent_mcp_url(&base_url, "sales-request-trace"); let response = post_jsonrpc_response( &client, &mcp_url, &api_key, None, Some("req_mcp_trace_123"), json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-11-25" } }), ) .await; assert_eq!( response.headers()["x-request-id"].to_str().unwrap(), "req_mcp_trace_123" ); assert_eq!(response.status(), reqwest::StatusCode::OK); let logs = writer.output(); let event = logs .lines() .filter_map(|line| serde_json::from_str::(line).ok()) .find(|event| event["event"] == "mcp.request.received") .unwrap(); assert_eq!(event["service"], "mcp-server"); assert_eq!(event["request_id"], "req_mcp_trace_123"); assert_eq!(event["fields"]["agent_slug"], "sales-request-trace"); assert_eq!(event["fields"]["workspace_slug"], "default"); assert_eq!(event["fields"]["jsonrpc_method"], "initialize"); }