|
| 1 | +use crate::errors::McpError; |
| 2 | +use crate::operations::operation_defs; |
| 3 | +use crate::schema_from_type; |
| 4 | +use apollo_compiler::Schema; |
| 5 | +use apollo_compiler::parser::Parser; |
| 6 | +use apollo_compiler::validation::Valid; |
| 7 | +use rmcp::model::CallToolResult; |
| 8 | +use rmcp::model::Content; |
| 9 | +use rmcp::model::{ErrorCode, Tool}; |
| 10 | +use rmcp::schemars::JsonSchema; |
| 11 | +use rmcp::serde_json::Value; |
| 12 | +use rmcp::{schemars, serde_json}; |
| 13 | +use serde::Deserialize; |
| 14 | +use std::default::Default; |
| 15 | +use std::sync::Arc; |
| 16 | +use tokio::sync::Mutex; |
| 17 | + |
| 18 | +/// The name of the tool to validate an ad hoc GraphQL operation |
| 19 | +pub const VALIDATE_TOOL_NAME: &str = "validate"; |
| 20 | + |
| 21 | +#[derive(Clone)] |
| 22 | +pub struct Validate { |
| 23 | + pub tool: Tool, |
| 24 | + schema: Arc<Mutex<Valid<Schema>>>, |
| 25 | +} |
| 26 | + |
| 27 | +/// Input for the validate tool |
| 28 | +#[derive(JsonSchema, Deserialize)] |
| 29 | +pub struct Input { |
| 30 | + /// The GraphQL operation |
| 31 | + operation: String, |
| 32 | +} |
| 33 | + |
| 34 | +impl Validate { |
| 35 | + pub fn new(schema: Arc<Mutex<Valid<Schema>>>) -> Self { |
| 36 | + Self { |
| 37 | + schema, |
| 38 | + tool: Tool::new( |
| 39 | + VALIDATE_TOOL_NAME, |
| 40 | + "Validates a GraphQL operation against the schema. \ |
| 41 | + Use the `introspect` tool first to get information about the GraphQL schema. \ |
| 42 | + Operations should be validated prior to calling the `execute` tool.", |
| 43 | + schema_from_type!(Input), |
| 44 | + ), |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Validates the provided GraphQL query |
| 49 | + pub async fn execute(&self, input: Value) -> Result<CallToolResult, McpError> { |
| 50 | + let input = serde_json::from_value::<Input>(input).map_err(|_| { |
| 51 | + McpError::new(ErrorCode::INVALID_PARAMS, "Invalid input".to_string(), None) |
| 52 | + })?; |
| 53 | + |
| 54 | + operation_defs(&input.operation, true, None) |
| 55 | + .map_err(|e| McpError::new(ErrorCode::INVALID_PARAMS, e.to_string(), None))? |
| 56 | + .ok_or_else(|| { |
| 57 | + McpError::new( |
| 58 | + ErrorCode::INVALID_PARAMS, |
| 59 | + "Invalid operation type".to_string(), |
| 60 | + None, |
| 61 | + ) |
| 62 | + })?; |
| 63 | + |
| 64 | + let schema_guard = self.schema.lock().await; |
| 65 | + Parser::new() |
| 66 | + .parse_executable(&schema_guard, input.operation.as_str(), "operation.graphql") |
| 67 | + .map_err(|e| McpError::new(ErrorCode::INVALID_PARAMS, e.to_string(), None))?; |
| 68 | + Ok(CallToolResult { |
| 69 | + content: vec![Content::text("Operation is valid")], |
| 70 | + is_error: None, |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use serde_json::json; |
| 78 | + |
| 79 | + use super::*; |
| 80 | + static SCHEMA: std::sync::LazyLock<Arc<Mutex<Valid<Schema>>>> = |
| 81 | + std::sync::LazyLock::new(|| { |
| 82 | + Arc::new(Mutex::new( |
| 83 | + Schema::parse_and_validate("type Query { id: ID! }", "schema.graphql").unwrap(), |
| 84 | + )) |
| 85 | + }); |
| 86 | + |
| 87 | + #[tokio::test] |
| 88 | + async fn validate_valid_query() { |
| 89 | + let validate = Validate::new(SCHEMA.clone()); |
| 90 | + let input = json!({ |
| 91 | + "operation": "query Test { id }" |
| 92 | + }); |
| 93 | + assert!(validate.execute(input).await.is_ok()); |
| 94 | + } |
| 95 | + |
| 96 | + #[tokio::test] |
| 97 | + async fn validate_invalid_graphql_query() { |
| 98 | + let validate = Validate::new(SCHEMA.clone()); |
| 99 | + let input = json!({ |
| 100 | + "operation": "query {" |
| 101 | + }); |
| 102 | + assert!(validate.execute(input).await.is_err()); |
| 103 | + } |
| 104 | + |
| 105 | + #[tokio::test] |
| 106 | + async fn validate_invalid_query_field() { |
| 107 | + let validate = Validate::new(SCHEMA.clone()); |
| 108 | + let input = json!({ |
| 109 | + "operation": "query { invalidField }" |
| 110 | + }); |
| 111 | + assert!(validate.execute(input).await.is_err()); |
| 112 | + } |
| 113 | +} |
0 commit comments