Returning serde_json::Value is not allowed? #913
Replies: 2 comments
|
Found a workaround: #[derive(Serialize, JsonSchema)]
#[serde(untagged)]
pub enum ExpressionResult {
Value(serde_json::Value),
}
#[derive(Serialize, JsonSchema)]
pub struct ExpressionResponse {
pub result: ExpressionResult,
}This works, seems unnecessary, but end result is equivalent. |
|
This is a schemars-side quirk rather than an rmcp or protocol restriction. For a field typed {
"type": "object",
"properties": { "result": true },
"required": ["result"]
}The tool's Your untagged-enum wrapper works only as a side effect: it turns the property into a If you'd rather not add the wrapper, point the field at an object-form "any" schema directly with use schemars::{Schema, SchemaGenerator};
fn any_json(_: &mut SchemaGenerator) -> Schema {
// {} is an object-form schema that still means "any JSON value"
Schema::try_from(serde_json::json!({})).unwrap()
}
#[derive(Serialize, JsonSchema)]
pub struct ExpressionResponse {
#[schemars(schema_with = "any_json")]
pub result: serde_json::Value,
}which emits: "properties": { "result": {} }If you'd prefer it self-documenting over a bare |
Uh oh!
There was an error while loading. Please reload this page.
Pre-submission Checklist
Question Category
Your Question
I have a tool that will return a value that is any serde_json::Value. So I define my struct like this:
This compiles fine, but using MCP Inspector. When I list tools I get this error:
If I change it to a String and convert the Value to a string, it works fine. However, this also means potential number values or objects, etc... will just be strings. I'd rather not return it as a JSON string since it seems unnatural to have the resulting JSON contain a property that contains a JSON string. What's the right way to do this? Thanks.
All reactions