Object of type AnyUrl is not JSON serializable while reading github file using MCP Tool
Describe the bug Object of type AnyUrl is not JSON serializable While reading the file from github using github MCP tool getting the above error. However in the response file content is correctly read.
To Reproduce Steps to reproduce the behavior:
- Create a MCP tool using the provided guideline.
github_mcp_agent = Agent( model=AGENT_MODEL, name='github_mcp_agent', instruction=GITHUB_MCP_AGENT_PROMPT, tools=[ MCPToolset( connection_params=StdioServerParameters( command='docker', args=[ "run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server" ], env={ "GITHUB_PERSONAL_ACCESS_TOKEN": github_personal_access_token } ) ) ],) - Try to read one of the file from the github repo (prefer any code file)
- in the traces call is successful and response if also successful. but then get an error Object of type AnyUrl is not JSON serializable.
- See error
Expected behavior Model should be able to provide the content of the file or whatever instruction are given. Like other calls are successful like listing the repo, listing files, commits etc.
Screenshots
Desktop (please complete the following information):
- OS: MacOS
- Python version(python -V): 3.13.3
- ADK version(pip show google-adk): 1.4.1
- Model : gemini-2.0-flash
Screenshot
I am also hitting this exact same error. Please let me know if there is anything i can do to help expedite a fix.
@gokul-ch-pandey @Adam-C-G Did you guys find any workaround for this issue?
@rakesh9700 No, i am not aware of any workarounds currently. Eager to help though if anyone can point me the right way?
@Adam-C-G I found that the uri returned in the response is a pydantic.AnyUrl, which fails during JSON serialization when passed to json.dumps.
Small helper function I used for converting AnyUrl instances to plain strings before serialization.
def convert_uri_to_string(self, obj):
if isinstance(obj, dict):
return {k: self.convert_uri_to_string(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [self.convert_uri_to_string(i) for i in obj]
elif hasattr(obj, "__str__") and type(obj).__name__ == "AnyUrl":
return str(obj)
return obj
And replace
data = json.dumps(http_request.data) if http_request.data else None
in google/genai/_api_client.py with
data = json.dumps(self.convert_uri_to_string(http_request.data)) if http_request.data else None
@rakesh9700 thankyou for that, i have implmented that locally and it seems to have fixed the issue.
For anyone who finds this and is using docker compose to build their ADK app, just add the following to your Dockerfile as a workaround:
# Fix the ADK implementation of URI handling for the github MCP
ADD _api_client.py ../usr/local/lib/python3.13/site-packages/google/genai/
While having copied an instance of _api_client.py from your venv to sit alongside the dockerfile and making the changes noted by @rakesh9700 above.
I hesitate to open a pull request to the repo with this fix embedded, as i am not a confident enough coder to know if it is optimal or not. (also dont want to steal Rakesh's credit) but this approach does seem to work if anyone wants to test and push it into the main build?
@rakesh9700 Thanks Man this solution worked for me.
@Jacksunwei Could you please take a look at the proposed solution here? We may need to create a PR to the underlying google-genai repo, specifically in this file: https://github.com/google/adk-python/issues/1540#issuecomment-3070100822
Done :)
https://github.com/googleapis/python-genai/pull/1524
@speedstorm1 - I can take a look at this
Hi @gokul-ch-pandey I think adk head should have fixed this issue with model_dump with mode="json".
https://github.com/google/adk-python/blob/22c6dbe83cd1a8900d0ac6fd23d2092f095189fa/src/google/adk/tools/mcp_tool/mcp_tool.py#L221-L222
You could check out my verification in
https://gist.github.com/Jacksunwei/de3708d2373d5767863dbfc9d56e5f57