Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using Map-based schema #52

Closed
habuma opened this issue Jan 5, 2025 · 0 comments
Closed

Error when using Map-based schema #52

habuma opened this issue Jan 5, 2025 · 0 comments
Assignees

Comments

@habuma
Copy link

habuma commented Jan 5, 2025

Following the example set in the sample application (at https://github.com/spring-projects-experimental/spring-ai-mcp/blob/main/spring-ai-mcp-sample/src/main/java/org/springframework/ai/mcp/sample/server/McpServerConfig.java#L135), I'm trying to specify the input schema with a Map. Specifically, if I specify the schema with Map.of("entityId", "String"), I get the following error:

{
  "error": {
    "message": "Invalid schema for function 'entity-live': schema must be a JSON Schema of 'type: \"object\"', got 'type: \"None\"'.",
    "type": "invalid_request_error",
    "param": "tools[1].function.parameters",
    "code": "invalid_function_parameters"
  }
}

However, it works fine if I specify the schema with a String, like this:

    private static final String COMMON_INPUT_SCHEMA = """
                                {
                                    "type": "object",
                                    "properties": {
                                        "entityId": {
                                            "type": "string",
                                            "description": "Entity ID"
                                        }
                                    },
                                    "required": ["entityId"]
                                }
                                """;
...
    private McpServer.ToolRegistration themeParkApiEntityScheduleToolRegistration() {
        return new McpServer.ToolRegistration(
                new McpSchema.Tool(
                        "entity-schedule",
                        "Return schedule data about an entity, including hours of operation",
                        COMMON_INPUT_SCHEMA),
                (arguments) -> {
                    String entityId = (String) arguments.get("entityId");
                    var entityScheduleJson = themeParkService.getEntitySchedule(entityId);
                    McpSchema.TextContent content = new McpSchema.TextContent(entityScheduleJson);
                    return new McpSchema.CallToolResult(List.of(content), false);
                });
    }

(The entire project can be found at https://github.com/habuma/spring-ai-examples/tree/main/spring-ai-mcp)

HOWEVER, I can get it to work with a Map-based schema is the Map is defined like this:

Map.of("type", "object", "properties",
                    Map.of("entityId",
                            Map.of("type", "string", "description", "Entity ID")), "required",
                    List.of("entityId"))

Which is, incidentally, the same Map that is produced internally when given the String schema above.

So, my take on this is that one of the two things is true:

  • The Spring AI MCP sample is incorrectly showing how to specify a Map-based schema.
  • The McpSchema.Tool should somehow wrap the incoming Map with the outer Map as the value of the "properties" key.
@tzolov tzolov self-assigned this Jan 6, 2025
tzolov added a commit that referenced this issue Jan 6, 2025
- Change Tool.inputSchema type from Map<String, Object> to String to store raw JSON schema
- Enhance StdioClientTransport error logging with more context
- Add ToolHelper utility class for:
  - Converting Spring AI FunctionCallbacks to MCP tools
  - Generating JSON schemas for tool input validation
  - Facilitating integration between Spring AI functions and MCP tools
- Update tests and samples to use the new Tool schema format

Resolves #52
@tzolov tzolov closed this as completed in aa26cfa Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants