Skip to content

Commit 358076e

Browse files
author
Matthew Hawkins
authored
Accept variables as string or json (#93)
1 parent f0ef95d commit 358076e

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

crates/apollo-mcp-server/src/introspection.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub struct Input {
145145
query: String,
146146

147147
/// The variable values
148-
variables: Option<String>,
148+
variables: Option<Value>,
149149
}
150150

151151
impl Execute {
@@ -182,14 +182,19 @@ impl graphql::Executable for Execute {
182182
let input = serde_json::from_value::<Input>(input).map_err(|_| {
183183
McpError::new(ErrorCode::INVALID_PARAMS, "Invalid input".to_string(), None)
184184
})?;
185-
input
186-
.variables
187-
.map(|v| {
188-
serde_json::from_str(&v).map_err(|_| {
189-
McpError::new(ErrorCode::INVALID_PARAMS, "Invalid input".to_string(), None)
190-
})
191-
})
192-
.unwrap_or(Ok(Value::Null))
185+
match input.variables {
186+
None => Ok(Value::Null),
187+
Some(Value::Null) => Ok(Value::Null),
188+
Some(Value::String(s)) => serde_json::from_str(&s).map_err(|_| {
189+
McpError::new(ErrorCode::INVALID_PARAMS, "Invalid input".to_string(), None)
190+
}),
191+
Some(obj) if obj.is_object() => Ok(obj),
192+
_ => Err(McpError::new(
193+
ErrorCode::INVALID_PARAMS,
194+
"Invalid input".to_string(),
195+
None,
196+
)),
197+
}
193198
}
194199
}
195200

@@ -200,7 +205,7 @@ mod tests {
200205
use rmcp::serde_json::json;
201206

202207
#[test]
203-
fn execute_query_with_variables() {
208+
fn execute_query_with_variables_as_string() {
204209
let execute = Execute::new(MutationMode::None);
205210

206211
let query = "query GetUser($id: ID!) { user(id: $id) { id name } }";
@@ -218,6 +223,25 @@ mod tests {
218223
assert_eq!(Executable::variables(&execute, input), Ok(variables));
219224
}
220225

226+
#[test]
227+
fn execute_query_with_variables_as_json() {
228+
let execute = Execute::new(MutationMode::None);
229+
230+
let query = "query GetUser($id: ID!) { user(id: $id) { id name } }";
231+
let variables = json!({ "id": "123" });
232+
233+
let input = json!({
234+
"query": query,
235+
"variables": variables
236+
});
237+
238+
assert_eq!(
239+
Executable::operation(&execute, input.clone()),
240+
Ok(query.to_string())
241+
);
242+
assert_eq!(Executable::variables(&execute, input), Ok(variables));
243+
}
244+
221245
#[test]
222246
fn execute_query_without_variables() {
223247
let execute = Execute::new(MutationMode::None);

0 commit comments

Comments
 (0)