Skip to content

Conversation

@dotsetgreg
Copy link

Summary

  • Fix "Resource already registered" error when calling tools like gzip-file-as-resource multiple times with the same output name
  • Track registered resources by URI in a module-level Map
  • Remove existing resource before registering a new one with the same URI

Problem

When a tool that creates session resources (like gzip-file-as-resource) is called multiple times with the same output name, the server throws:

Error: Resource demo://resource/session/README.md.gz is already registered

This is particularly problematic because:

  1. The gzip-file-as-resource tool has a default name of README.md.gz
  2. LLM agents often retry tool calls or call tools multiple times with default parameters
  3. There was no way to update or overwrite an existing resource

Solution

The fix tracks registered session resources by URI in a Map, and before registering a new resource, it checks if one already exists with the same URI. If so, it uses the SDK's remove() method to unregister the old resource first, then registers the new one.

Test plan

  • Called gzip-file-as-resource 10 times with default parameters - all succeed
  • Called with same explicit name multiple times - succeeds
  • Build passes with no TypeScript errors
  • Verified using Bellwether MCP server validation tool

How this was found

This bug was discovered using Bellwether, an open-source MCP server validation and documentation tool. Bellwether runs automated schema validation and behavioral testing against MCP servers, which caught this regression when repeatedly testing the gzip-file-as-resource tool.

🤖 Generated with Claude Code

When a tool like `gzip-file-as-resource` is called multiple times with the
same output name (especially the default `README.md.gz`), the server would
throw "Resource already registered" because the SDK doesn't allow
registering duplicate URIs.

This fix:
- Tracks registered resources by URI in a module-level Map
- Before registering a new resource, checks if the URI already exists
- If it does, removes the old resource using the SDK's `remove()` method
- Then registers the new resource with fresh content

This allows tools to be called repeatedly with the same parameters without
errors, which is important for LLM agents that may retry tool calls.

Found using Bellwether (https://bellwether.sh), an MCP server validation tool.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

1 participant