-
Notifications
You must be signed in to change notification settings - Fork 495
Fix: Functions Passed to Agents Are Now Available as Tools #120
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix @rholinshead! I think your fix uncovered another issue which is that initialize shouldn't be calling aenter, but rather an aenter should be calling initialize. That mistake is why this wasn't working as intended.
functions=[add_numbers, multiply_numbers], | ||
) | ||
|
||
async with math_agent: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should initialize the agent
src/mcp_agent/agents/agent.py
Outdated
@@ -79,10 +82,6 @@ async def initialize(self): | |||
self.__aenter__() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to change the initialize function to get rid of this, and instead to implement the aenter asynccontextmanager for this class, which a) calls super.aenter, and b) calls initialize.
I think for now initialize can stay in empty and just call super.initialize()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I looked into this and:
- confirmed that implementing
__aenter__
and calling initialize with the function tool map construction in initialize (to test) worked as expected, e.g.
async def __aenter__(self):
"""
Initialize the agent and connect to the MCP servers.
NOTE: This method is called automatically when the agent is used as an async context manager.
"""
if self.initialized:
return self
await super().__aenter__()
await self.initialize()
return self
async def initialize(self):
for function in self.functions:
tool: FastTool = FastTool.from_function(function)
self._function_tool_map[tool.name] = tool
But, having the function tool map construction in __init__
IMO makes sense (sounds like we agree). Looks like there is no initialize
in any superclass so super().initialize()
is invalid. In that case, we'll end up with an empty initialize
and then __aenter__
becomes a redundant override.
So, might make sense to just remove __aenter__
and initialize
from this class for now and add back in the future if needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saqadri lastest version here removes them as redundant. If you'd prefer to have them just to be explicit / to allow future updates to initialize please let me know and I can add them back
@@ -64,6 +64,9 @@ def __init__( | |||
|
|||
# Map function names to tools | |||
self._function_tool_map: Dict[str, FastTool] = {} | |||
for function in self.functions: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good change to have in the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
* fix/agent-functions * Remove redundant __aenter__ and initialize
Summary
Currently, functions passed to agents do not properly get listed as tools since the async
initialize
is not (always?) called. To fix, pull the_function_tool_map
construction into__init__
.Changes
_function_tool_map
in__init__
inagent.py
functions
example to show working usageTest Plan
See our functions listed as tools:
See them being called as tools:
And correct response: