Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] MCP ToolCollection raises KeyError: 'type' #626

Closed
gonzoramos opened this issue Feb 12, 2025 · 8 comments · Fixed by #895
Closed

[BUG] MCP ToolCollection raises KeyError: 'type' #626

gonzoramos opened this issue Feb 12, 2025 · 8 comments · Fixed by #895
Assignees
Labels
bug Something isn't working

Comments

@gonzoramos
Copy link

gonzoramos commented Feb 12, 2025

Describe the bug
When loading an MCP tool (e.g. mcp_server_tavily), an exception occurs in _generate_tool_inputs(...). key "type" is not found.

Code to reproduce the error

# Load an MCP server like so
server_parameters = StdioServerParameters(
    command="python",
    args=["mcp_server_tavily.py"],
)

# then see what tools it exposes
with ToolCollection.from_mcp(server_parameters) as toolset:

    for tool in [*toolset.tools]:
        print(tool.name)

Error logs (if any)

  File ".../lib/python3.11/site-packages/mcpadapt/smolagents_adapter.py", line 30, in <dictcomp>
    k: {"type": v["type"], "description": v.get("description", "")}
                ~^^^^^^^^
KeyError: 'type'

Expected behavior
No exception

Packages version:
smolagents==1.8.1

Additional context
Add any other context about the problem here.

@gonzoramos gonzoramos added the bug Something isn't working label Feb 12, 2025
@gonzoramos
Copy link
Author

This edit fixed the exception, but I am not sure if it causes a regression:

def _generate_tool_inputs(resolved_json_schema: dict[str, Any]) -> dict[str, str]:
    """
    takes an json_schema as used in the MCP protocol and return an inputs dict for
    smolagents tools. see AUTHORIZED_TYPES in smolagents.tools for the types allowed.
    Note that we consider the json_schema to already have $ref resolved with jsonref for
    example.
    """
    
    # debuging
    items = resolved_json_schema.items()
    print(items)

    return {
        # TODO: use google-docstring-parser to parse description of args and pass it here...
        k: {"type": v["type"] if "type" in v else "object", "description": v.get("description", "")}
        for k, v in resolved_json_schema.items()
    }

@aymeric-roucher
Copy link
Collaborator

@grll what do you think, should we merge this change?

@sysradium
Copy link
Contributor

I would carefully check beforehand : 🤔

@grll
Copy link
Contributor

grll commented Feb 13, 2025

I need to find the root cause first, is it because we are not exposing the type as we should in mcpadapt?

@albertvillanova albertvillanova changed the title [BUG] [BUG] MCP ToolCollection raises KeyError: 'type' Feb 14, 2025
@phirsch
Copy link

phirsch commented Feb 24, 2025

FWIW, during troubleshooting when I encountered this bug, I found that other tools like @wong2/mcp-cli also complained about missing type fields with the MCP(s) I used (in my case https://github.com/YUZongmin/semantic-scholar-fastmcp-mcp-server, https://github.com/afrise/academic-search-mcp-server, https://github.com/ivo-toby/mcp-openapi-server).

@grll
Copy link
Contributor

grll commented Mar 4, 2025

Hey there I have got some news on this issue @murawakimitsuhiro flagged it as mcpadapt not correctly handling mcp server where some parameters are nullable, see grll/mcpadapt#10. We are working on a fix.

@grll
Copy link
Contributor

grll commented Mar 5, 2025

We have successfully merged the work of @murawakimitsuhiro which means this should be fixed by bumping mcpadapt to 0.0.15 and above.

@albertvillanova
Copy link
Member

Thanks for the fix, @murawakimitsuhiro and @grll! 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants