Skip to content

test: add regression tests for tool schema serialization (#671) - #739

Merged
ezynda3 merged 1 commit into
mark3labs:mainfrom
koriyoshi2041:fix/schema-serialization
Mar 9, 2026
Merged

ezynda3 merged 1 commit into
mark3labs:mainfrom
koriyoshi2041:fix/schema-serialization

Conversation

@koriyoshi2041

@koriyoshi2041 koriyoshi2041 commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Description

Add regression tests to verify that Tool inputSchema serialization produces correct JSON Schema output without double-wrapping the schema inside an extra "properties" key, as reported in #671.

The issue described a scenario where inputSchema would be incorrectly serialized as:

{
  "properties": {
    "type": "object",
    "properties": { ... },
    "required": [...]
  }
}

instead of the correct:

{
  "type": "object",
  "properties": { ... },
  "required": [...]
}

While the maintainer confirmed the bug could not be reproduced on the current codebase (and the root cause was likely addressed by #713 which added proper MarshalJSON/UnmarshalJSON for ToolInputSchema), there were no regression tests guarding against this specific failure mode.

This PR adds three test functions:

  • TestToolInputSchema_NoDoubleWrapping_Issue671 - Table-driven test covering 4 tool construction methods (WithString params, mixed types, no params, manually constructed schema), each verifying the serialized inputSchema has type at the top level and properties does not contain schema-level keys.
  • TestToolSchema_MarshalUnmarshal_RoundTrip_Issue671 - Verifies marshal -> unmarshal -> marshal produces structurally identical inputSchema.
  • TestListToolsResult_Schema_Issue671 - Verifies correct serialization when tools are nested inside ListToolsResult (the actual server response path).

Fixes #671

Type of Change

  • Tests only (no functional changes)

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly

Summary by CodeRabbit

  • Tests
    • Added validation tests for Tool schema handling to ensure proper structure and JSON serialization integrity.

Add tests to verify that Tool inputSchema serialization does not
double-wrap the schema inside an extra "properties" key, as reported
in issue mark3labs#671.

Three test functions cover:
- Direct marshaling with various tool construction methods
- Marshal/unmarshal round-trip consistency
- ListToolsResult serialization (the actual server response path)

Closes mark3labs#671
@coderabbitai

coderabbitai Bot commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Adds three test cases to mcp/tools_test.go to validate Tool inputSchema handling for Issue 671, covering schema structure validation, JSON marshal/unmarshal round-trip behavior, and ListToolsResult serialization. The test blocks appear twice in the diff, creating duplicate test declarations.

Changes

Cohort / File(s) Summary
Tool Schema Test Cases (Issue 671)
mcp/tools_test.go
Adds TestToolInputSchema_NoDoubleWrapping_Issue671, TestToolSchema_MarshalUnmarshal_RoundTrip_Issue671, and TestListToolsResult_Schema_Issue671 to validate inputSchema structure integrity and JSON round-trip stability. Test blocks are duplicated in the diff.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding regression tests for tool schema serialization, with direct reference to issue #671.
Description check ✅ Passed The PR description is comprehensive, includes the issue context, explains the expected vs actual behavior, clearly documents the three test functions being added, and has all required checklist items addressed.
Linked Issues check ✅ Passed The PR successfully addresses issue #671 by adding three regression tests that validate correct Tool inputSchema serialization without double-wrapping and verify round-trip stability.
Out of Scope Changes check ✅ Passed The PR only adds test code with no functional changes to the codebase, which is entirely within scope for addressing the regression testing requirement from issue #671.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
mcp/tools_test.go (2)

2338-2339: Consider using guarded type assertions for robustness.

These unguarded type assertions will panic if the structure is unexpected. Using require.True with the ok idiom would provide clearer test failure messages.

🔧 Suggested fix
-	schema1 := parsed1["inputSchema"].(map[string]any)
-	schema2 := parsed2["inputSchema"].(map[string]any)
+	schema1, ok := parsed1["inputSchema"].(map[string]any)
+	require.True(t, ok, "parsed1 inputSchema should be a map")
+	schema2, ok := parsed2["inputSchema"].(map[string]any)
+	require.True(t, ok, "parsed2 inputSchema should be a map")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mcp/tools_test.go` around lines 2338 - 2339, Replace the unguarded type
assertions for parsed1 and parsed2 when creating schema1 and schema2 with the
"ok" idiom and test assertions: check that parsed1["inputSchema"] and
parsed2["inputSchema"] are maps using the two-value assertion (v, ok :=
parsedX["inputSchema"].(map[string]any)) and use require.True(ok, ...) (or
require.IsType/require.NotNil as preferred) to produce clear test failures
instead of allowing a panic; update the references to schema1 and schema2 to use
the guarded variables after the assertion succeeds.

2369-2378: Multiple unguarded type assertions could panic on unexpected structures.

Using the ok idiom with require.True would provide clearer failure messages if the JSON structure changes unexpectedly.

🔧 Suggested fix for key assertions
-	tools := parsed["tools"].([]any)
+	tools, ok := parsed["tools"].([]any)
+	require.True(t, ok, "tools should be an array")
 	require.Len(t, tools, 1)

-	toolMap := tools[0].(map[string]any)
-	inputSchema := toolMap["inputSchema"].(map[string]any)
+	toolMap, ok := tools[0].(map[string]any)
+	require.True(t, ok, "tool should be a map")
+	inputSchema, ok := toolMap["inputSchema"].(map[string]any)
+	require.True(t, ok, "inputSchema should be a map")

 	assert.Equal(t, "object", inputSchema["type"],
 		"inputSchema.type should be 'object'")

-	props := inputSchema["properties"].(map[string]any)
+	props, ok := inputSchema["properties"].(map[string]any)
+	require.True(t, ok, "properties should be a map")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mcp/tools_test.go` around lines 2369 - 2378, The test uses unguarded type
assertions for parsed["tools"], tools[0], inputSchema and properties which can
panic; replace each direct assertion with the comma-ok form and assert the ok
with require.True (or require.IsType) to provide clear failures: e.g., check
parsed["tools"] returns a []any ok, then require.Len on that slice; check
tools[0] is a map[string]any into toolMap with ok and require.True; check
toolMap["inputSchema"] is a map[string]any into inputSchema with ok and
require.True; and check inputSchema["properties"] is a map[string]any into props
with ok and require.True, keeping the existing assert.Equal for
inputSchema["type"] after verifying the type.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@mcp/tools_test.go`:
- Around line 2338-2339: Replace the unguarded type assertions for parsed1 and
parsed2 when creating schema1 and schema2 with the "ok" idiom and test
assertions: check that parsed1["inputSchema"] and parsed2["inputSchema"] are
maps using the two-value assertion (v, ok :=
parsedX["inputSchema"].(map[string]any)) and use require.True(ok, ...) (or
require.IsType/require.NotNil as preferred) to produce clear test failures
instead of allowing a panic; update the references to schema1 and schema2 to use
the guarded variables after the assertion succeeds.
- Around line 2369-2378: The test uses unguarded type assertions for
parsed["tools"], tools[0], inputSchema and properties which can panic; replace
each direct assertion with the comma-ok form and assert the ok with require.True
(or require.IsType) to provide clear failures: e.g., check parsed["tools"]
returns a []any ok, then require.Len on that slice; check tools[0] is a
map[string]any into toolMap with ok and require.True; check
toolMap["inputSchema"] is a map[string]any into inputSchema with ok and
require.True; and check inputSchema["properties"] is a map[string]any into props
with ok and require.True, keeping the existing assert.Equal for
inputSchema["type"] after verifying the type.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2af37ff5-314e-4bed-a59e-b5bff8e1a7c6

📥 Commits

Reviewing files that changed from the base of the PR and between 37c4d97 and 61722ee.

📒 Files selected for processing (1)
  • mcp/tools_test.go

@ezynda3
ezynda3 merged commit c70aadc into mark3labs:main Mar 9, 2026
4 checks passed
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 this pull request may close these issues.

bug: MCP-Go Library Tool Schema Serialization Bug

2 participants