Skip to content

Conversation

@rhtnr
Copy link

@rhtnr rhtnr commented Dec 20, 2025

Summary

Fixes #703

Adds support for configuring custom request ID generators, enabling compatibility with MCP servers that require specific ID formats (e.g., numeric-only IDs like the Snowflake MCP server).

Changes

  • Added RequestIdGenerator functional interface in io.modelcontextprotocol.spec with:
    • generate() method for creating unique request IDs
    • ofDefault() factory method for UUID-prefixed incrementing IDs (existing behavior)
    • ofIncremental() factory method for simple numeric IDs
  • Updated McpClientSession to accept a custom RequestIdGenerator
  • Added requestIdGenerator() builder methods to both McpClient.SyncSpec and McpClient.AsyncSpec
  • Added 3 comprehensive tests for custom ID generators

Usage Example

// Using the built-in numeric ID generator
McpClient.sync(transport)
    .requestIdGenerator(RequestIdGenerator.ofIncremental())
    .build();

// Using a custom generator
AtomicLong counter = new AtomicLong(0);
McpClient.async(transport)
    .requestIdGenerator(() -> String.valueOf(counter.incrementAndGet()))
    .build();

Backward Compatibility

This change is fully backward compatible:

  • Existing code continues to work without changes
  • Default behavior remains the same (UUID-prefixed incrementing IDs)
  • New requestIdGenerator() method is optional

Test plan

  • All 3 new tests pass for custom ID generators
  • All 89 existing schema and session tests pass
  • Project compiles successfully

Adds support for configuring custom request ID generators, enabling
compatibility with MCP servers that require specific ID formats (e.g.,
numeric-only IDs).

Changes:
- Add RequestIdGenerator functional interface with factory methods
  for default (UUID-prefixed) and incremental (numeric) generators
- Update McpClientSession to accept a custom RequestIdGenerator
- Add requestIdGenerator() builder methods to McpClient.SyncSpec
  and McpClient.AsyncSpec
- Add comprehensive tests for custom ID generators
@He-Pin
Copy link
Contributor

He-Pin commented Dec 20, 2025

This is actually a nice addition, thanks.

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.

Allow custom or numeric request ID generation (Snowflake MCP server rejects string IDs)

2 participants