Skip to content

Allow alternative serialization to str when objects are returned #207

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

Open
strawgate opened this issue Apr 18, 2025 · 1 comment
Open

Allow alternative serialization to str when objects are returned #207

strawgate opened this issue Apr 18, 2025 · 1 comment

Comments

@strawgate
Copy link
Contributor

strawgate commented Apr 18, 2025

Right now there's a content formatter that gets called when tools return. Anything other than a string basically.

The content formatter just dumps it to JSON

It would be nice if this was customizable. At the moment I have to wrap all my tool return calls in a formatter call to return something other than compressed json

For example, I'd like to return yaml from tool calls as that tends to be readable for humans and for machines or alternatively maybe I'd like to return prettified json

Perhaps a configurable formatting callable that defaults to dumping json?

@strawgate
Copy link
Contributor Author

strawgate commented Apr 19, 2025

For now I'm doing:


        # We need to wrap the underlying call in a way that captures the response and then calls the formatter.
        def format_response(func: Callable) -> Callable:
            @functools.wraps(func)
            async def wrapper(*args, **kwargs):
                response = await func(*args, **kwargs)
                return self.response_formatter(response)

            return wrapper

        # Register the tools with the MCP server.
        mcp.add_tool(format_response(self.knowledge_base_client.create))
        mcp.add_tool(format_response(self.knowledge_base_client.get))

I would really like to switch to using the mixin and the decorators and will try to figure out if I can do this probably by nesting the decorators

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

No branches or pull requests

1 participant