Skip to content

Conversation

CocaineCong
Copy link
Contributor

@CocaineCong CocaineCong commented Jul 2, 2025

Description

Fixes #414

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • MCP spec compatibility implementation
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Performance improvement
  • Tests only (no functional changes)
  • Other (please describe):

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

MCP Spec Compliance

  • This PR implements a feature defined in the MCP specification
  • Link to relevant spec section: Link text
  • Implementation follows the specification exactly

Additional Information

what's changes following:

  • Add support in tool result handling
  • Update examples to show resource link
  • Add tests for resource link functionality

Summary by CodeRabbit

  • New Features

    • Added a new tool that provides resource links, allowing users to access files such as documents directly from the server.
    • Enhanced tool responses to include resource links alongside other content types.
  • Documentation

    • Updated documentation with examples on how to use resource links in tool responses, including combining resource links with text and adding metadata annotations.
  • Tests

    • Introduced tests to ensure correct handling and serialization of resource links and mixed content in tool responses.

Copy link
Contributor

coderabbitai bot commented Jul 2, 2025

Walkthrough

A new tool for returning resource links was added to the MCP server, including handler logic and example usage. JSON marshaling and unmarshaling for CallToolResult and Content types were customized to support ResourceLink. Tests and documentation were updated to demonstrate and validate resource link handling in tool results.

Changes

Files/Paths Change Summary
examples/everything/main.go Added "get_resource_link" tool and handler for returning resource links in tool results.
mcp/tools.go, mcp/types.go Implemented custom JSON marshal/unmarshal for CallToolResult and Content (including ResourceLink).
mcp/types_test.go Added tests for ResourceLink serialization and tool result content round-tripping.
www/docs/pages/servers/tools.mdx Extended documentation with examples and explanations for resource links in tool results.

Assessment against linked issues

Objective (issue #) Addressed Explanation
Add ResourceLink type with discriminator (#414)
Update ContentBlock union to include ResourceLink (#414)
Add support in tool result handling (#414)
Update examples to show resource linking (#414)
Add tests for resource link functionality (#414)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Possibly related PRs

  • mark3labs/mcp-go#407: Builds upon the ResourceLink type and parsing support, directly utilizing ResourceLink content type and its JSON marshaling/unmarshaling enhancements.
  • mark3labs/mcp-go#26: Changes the Content field type to []Content, directly related to how this PR handles and serializes content in tool results.

Suggested reviewers

  • dugenkui03
  • ezynda3

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0e457af and dcb737d.

📒 Files selected for processing (2)
  • mcp/types_test.go (1 hunks)
  • www/docs/pages/servers/tools.mdx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • mcp/types_test.go
  • www/docs/pages/servers/tools.mdx
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
mcp/types.go (1)

1068-1103: Minor documentation inconsistency in the switch case.

The implementation is correct, but the comment mentions EmbeddedResource while the actual type string in the switch case is "resource". Consider updating the comment to match the actual type string used.

mcp/types_test.go (1)

97-138: Consider verifying all ResourceLink fields after unmarshaling.

The test correctly verifies the mixed content handling, but only checks some fields of the ResourceLink. Consider also asserting the Description and MIMEType fields for completeness.

 	// Check second content (ResourceLink)
 	resourceLink, ok := unmarshalled.Content[1].(ResourceLink)
 	require.True(t, ok)
 	assert.Equal(t, "resource_link", resourceLink.Type)
 	assert.Equal(t, "file:///example/test.pdf", resourceLink.URI)
 	assert.Equal(t, "Test Document", resourceLink.Name)
+	assert.Equal(t, "A test document", resourceLink.Description)
+	assert.Equal(t, "application/pdf", resourceLink.MIMEType)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 251da13 and 0e457af.

📒 Files selected for processing (5)
  • examples/everything/main.go (1 hunks)
  • mcp/tools.go (1 hunks)
  • mcp/types.go (1 hunks)
  • mcp/types_test.go (1 hunks)
  • www/docs/pages/servers/tools.mdx (1 hunks)
🧰 Additional context used
🧠 Learnings (6)
📓 Common learnings
Learnt from: floatingIce91
PR: mark3labs/mcp-go#401
File: server/server.go:1082-1092
Timestamp: 2025-06-23T11:10:42.948Z
Learning: In Go MCP server, ServerTool.Tool field is only used for tool listing and indexing, not for tool execution or middleware. During handleToolCall, only the Handler field is used, so dynamic tools don't need the Tool field populated.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:107-137
Timestamp: 2025-03-04T06:59:43.882Z
Learning: Tool responses from the MCP server shouldn't contain RawInputSchema, which is why the UnmarshalJSON method for the Tool struct is implemented to handle only the structured InputSchema format.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in the mark3labs/mcp-go project should handle both InputSchema and RawInputSchema consistently between MarshalJSON and UnmarshalJSON methods, even though the tools response from MCP server typically doesn't contain rawInputSchema.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in mark3labs/mcp-go handles both InputSchema and RawInputSchema formats. When unmarshaling JSON, it first tries to parse into a structured ToolInputSchema format, and if that fails, it falls back to using the raw schema format, providing symmetry with the MarshalJSON method.
mcp/types_test.go (2)
Learnt from: octo
PR: mark3labs/mcp-go#149
File: mcptest/mcptest.go:0-0
Timestamp: 2025-04-21T21:26:32.945Z
Learning: In the mcptest package, prefer returning errors from helper functions rather than calling t.Fatalf() directly, giving callers flexibility in how to handle errors.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in the mark3labs/mcp-go project should handle both InputSchema and RawInputSchema consistently between MarshalJSON and UnmarshalJSON methods, even though the tools response from MCP server typically doesn't contain rawInputSchema.
mcp/types.go (5)
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in the mark3labs/mcp-go project should handle both InputSchema and RawInputSchema consistently between MarshalJSON and UnmarshalJSON methods, even though the tools response from MCP server typically doesn't contain rawInputSchema.
Learnt from: ezynda3
PR: mark3labs/mcp-go#461
File: server/sampling.go:22-26
Timestamp: 2025-06-30T07:13:16.959Z
Learning: In the mark3labs/mcp-go project, the MCPServer.capabilities field is a struct value (serverCapabilities), not a pointer, so it cannot be nil and doesn't require nil checking. Only pointer fields within the capabilities struct should be checked for nil.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:107-137
Timestamp: 2025-03-04T06:59:43.882Z
Learning: Tool responses from the MCP server shouldn't contain RawInputSchema, which is why the UnmarshalJSON method for the Tool struct is implemented to handle only the structured InputSchema format.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in mark3labs/mcp-go handles both InputSchema and RawInputSchema formats. When unmarshaling JSON, it first tries to parse into a structured ToolInputSchema format, and if that fails, it falls back to using the raw schema format, providing symmetry with the MarshalJSON method.
Learnt from: octo
PR: mark3labs/mcp-go#149
File: mcptest/mcptest.go:0-0
Timestamp: 2025-04-21T21:26:32.945Z
Learning: In the mcptest package, prefer returning errors from helper functions rather than calling t.Fatalf() directly, giving callers flexibility in how to handle errors.
mcp/tools.go (4)
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:107-137
Timestamp: 2025-03-04T06:59:43.882Z
Learning: Tool responses from the MCP server shouldn't contain RawInputSchema, which is why the UnmarshalJSON method for the Tool struct is implemented to handle only the structured InputSchema format.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in the mark3labs/mcp-go project should handle both InputSchema and RawInputSchema consistently between MarshalJSON and UnmarshalJSON methods, even though the tools response from MCP server typically doesn't contain rawInputSchema.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in mark3labs/mcp-go handles both InputSchema and RawInputSchema formats. When unmarshaling JSON, it first tries to parse into a structured ToolInputSchema format, and if that fails, it falls back to using the raw schema format, providing symmetry with the MarshalJSON method.
Learnt from: floatingIce91
PR: mark3labs/mcp-go#401
File: server/server.go:1082-1092
Timestamp: 2025-06-23T11:10:42.948Z
Learning: In Go MCP server, ServerTool.Tool field is only used for tool listing and indexing, not for tool execution or middleware. During handleToolCall, only the Handler field is used, so dynamic tools don't need the Tool field populated.
www/docs/pages/servers/tools.mdx (5)
Learnt from: floatingIce91
PR: mark3labs/mcp-go#401
File: server/server.go:1082-1092
Timestamp: 2025-06-23T11:10:42.948Z
Learning: In Go MCP server, ServerTool.Tool field is only used for tool listing and indexing, not for tool execution or middleware. During handleToolCall, only the Handler field is used, so dynamic tools don't need the Tool field populated.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:107-137
Timestamp: 2025-03-04T06:59:43.882Z
Learning: Tool responses from the MCP server shouldn't contain RawInputSchema, which is why the UnmarshalJSON method for the Tool struct is implemented to handle only the structured InputSchema format.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in the mark3labs/mcp-go project should handle both InputSchema and RawInputSchema consistently between MarshalJSON and UnmarshalJSON methods, even though the tools response from MCP server typically doesn't contain rawInputSchema.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:0-0
Timestamp: 2025-03-04T07:00:57.111Z
Learning: The Tool struct in mark3labs/mcp-go handles both InputSchema and RawInputSchema formats. When unmarshaling JSON, it first tries to parse into a structured ToolInputSchema format, and if that fails, it falls back to using the raw schema format, providing symmetry with the MarshalJSON method.
Learnt from: octo
PR: mark3labs/mcp-go#149
File: mcptest/mcptest.go:0-0
Timestamp: 2025-04-21T21:26:32.945Z
Learning: In the mcptest package, prefer returning errors from helper functions rather than calling t.Fatalf() directly, giving callers flexibility in how to handle errors.
examples/everything/main.go (3)
Learnt from: floatingIce91
PR: mark3labs/mcp-go#401
File: server/server.go:1082-1092
Timestamp: 2025-06-23T11:10:42.948Z
Learning: In Go MCP server, ServerTool.Tool field is only used for tool listing and indexing, not for tool execution or middleware. During handleToolCall, only the Handler field is used, so dynamic tools don't need the Tool field populated.
Learnt from: xinwo
PR: mark3labs/mcp-go#35
File: mcp/tools.go:107-137
Timestamp: 2025-03-04T06:59:43.882Z
Learning: Tool responses from the MCP server shouldn't contain RawInputSchema, which is why the UnmarshalJSON method for the Tool struct is implemented to handle only the structured InputSchema format.
Learnt from: ezynda3
PR: mark3labs/mcp-go#461
File: server/sampling.go:22-26
Timestamp: 2025-06-30T07:13:16.959Z
Learning: In the mark3labs/mcp-go project, the MCPServer.capabilities field is a struct value (serverCapabilities), not a pointer, so it cannot be nil and doesn't require nil checking. Only pointer fields within the capabilities struct should be checked for nil.
🧬 Code Graph Analysis (3)
mcp/types_test.go (2)
mcp/utils.go (1)
  • NewResourceLink (226-234)
mcp/types.go (5)
  • ResourceLink (869-880)
  • ResourceLink (882-882)
  • Content (827-829)
  • TextContent (833-838)
  • TextContent (840-840)
mcp/tools.go (1)
mcp/types.go (2)
  • Content (827-829)
  • UnmarshalContent (1068-1103)
examples/everything/main.go (3)
mcp/tools.go (7)
  • NewTool (635-657)
  • WithDescription (678-682)
  • WithString (913-931)
  • Description (737-741)
  • DefaultString (765-769)
  • CallToolRequest (46-49)
  • CallToolResult (36-43)
mcp/types.go (3)
  • Content (827-829)
  • TextContent (833-838)
  • TextContent (840-840)
mcp/utils.go (1)
  • NewResourceLink (226-234)
🔇 Additional comments (7)
mcp/types.go (1)

1062-1065: LGTM!

Simple and clean wrapper for Content marshaling.

mcp/types_test.go (1)

72-95: Well-structured test for ResourceLink serialization.

The test properly verifies round-trip JSON serialization for all ResourceLink fields.

mcp/tools.go (2)

464-486: Clean implementation of conditional field marshaling.

The method correctly handles optional fields by only including them when they have meaningful values (Meta when non-nil, IsError when true).


488-528: Robust unmarshaling with proper type handling.

The implementation correctly handles the polymorphic Content array by leveraging the UnmarshalContent function. While re-marshaling items might seem inefficient, it's a clean approach that ensures proper type discrimination.

examples/everything/main.go (2)

168-173: LGTM! Tool registration follows best practices.

The tool is properly configured with a descriptive name, clear description, and a string parameter with a sensible default value.


178-201: Excellent example of mixed content with resource links.

The handler effectively demonstrates:

  • Parameter extraction with defaults
  • Creating mixed content arrays
  • Dynamic URI construction
  • Proper ResourceLink initialization with all fields
www/docs/pages/servers/tools.mdx (1)

532-535: Clear introduction to resource links concept.

The explanation effectively communicates the purpose and benefits of using resource links.

Signed-off-by: FanOne <294350394@qq.com>
@ezynda3 ezynda3 merged commit e859847 into mark3labs:main Jul 15, 2025
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.

Task: Add Resource Links in Tool Results
2 participants