fix: serialize Pydantic AnyUrl fields when persisting MCP OAuth state - #5677
Closed
amanuel2 wants to merge 1 commit into
Closed
fix: serialize Pydantic AnyUrl fields when persisting MCP OAuth state#5677amanuel2 wants to merge 1 commit into
amanuel2 wants to merge 1 commit into
Conversation
OAuth client information and token responses from the MCP SDK contain Pydantic AnyUrl fields (client_uri, redirect_uris, etc.). The previous model_dump() call returned a dict with these AnyUrl objects still as their native Python type, which then crashed json.dumps with: TypeError: Object of type AnyUrl is not JSON serializable This caused any OAuth-based MCP server (e.g. alphaxiv) to fail registration with an "OAuth flow error" traceback during startup. Adding mode="json" tells Pydantic to serialize all fields to JSON-compatible primitives (AnyUrl -> str, datetime -> ISO string, etc.) before returning the dict, so the standard json.dumps can handle it. Three call sites fixed: - HermesTokenStorage.set_tokens - HermesTokenStorage.set_client_info - build_oauth_auth pre-registration write
amanuel2
marked this pull request as ready for review
April 7, 2026 01:05
This was referenced Apr 24, 2026
Contributor
|
Thanks for this fix, @amanuel2! The change has already been merged to
Closing as implemented. Automated hermes-sweeper review. |
Collaborator
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TypeError: Object of type AnyUrl is not JSON serializablecrash when persisting OAuth state for MCP serverstools/mcp_oauth.pyusemodel_dump(exclude_none=True)which leaves PydanticAnyUrlobjects in the returned dict, breaking the subsequentjson.dumpsReproduction
Configure any OAuth-based MCP server (e.g. alphaxiv) and start hermes:
The MCP server fails registration and is marked as
failedin the MCP servers list.Root Cause
OAuthClientInformationFullandOAuthTokenfrom the MCP SDK contain Pydantic fields typed asAnyUrl(e.g.client_uri,redirect_uris,jwks_uri). Pydantic's defaultmodel_dump()preserves these as nativeAnyUrlinstances rather than converting them to strings. The standard libraryjson.dumpsthen has no way to serialize them.Fix
Use
model_dump(mode="json", ...)instead. Themode="json"flag tells Pydantic to convert all fields to JSON-compatible primitives (AnyUrl→str,datetime→ ISO string, etc.) before returning the dict, so the existing_write_jsonhelper works without further changes.Three call sites fixed:
HermesTokenStorage.set_tokens(line 206)HermesTokenStorage.set_client_info(line 222)build_oauth_authpre-registration write (line 465)Test plan
OAuth flow errortracebackget_client_info/get_tokensround-trip correctly throughmodel_validateDiscovered while running hermes with the alphaxiv MCP server configured.