-
Notifications
You must be signed in to change notification settings - Fork 3.1k
add MCPToolset #5138
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
Merged
Merged
add MCPToolset #5138
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d33f002
add MCPToolset
longcw 35ddc01
update setup and aclose
longcw 45549f0
update example
longcw ad50d39
reset _initialized
longcw df1afbd
fix types
longcw d894714
fix import
longcw 6cc6343
update doc
longcw bedc488
Merge remote-tracking branch 'origin/main' into longc/improve-toolsets
longcw 135a513
move toolsets to llm
longcw c2c907b
move MCPToolset to mcp
longcw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import functools | ||
| import inspect | ||
| import itertools | ||
|
|
@@ -47,7 +48,7 @@ def id(self) -> str: | |
| return self._id | ||
|
|
||
|
|
||
| class Toolset(ABC): | ||
| class Toolset: | ||
| @dataclass | ||
| class ToolCalledEvent: | ||
| ctx: RunContext | ||
|
|
@@ -58,16 +59,39 @@ class ToolCompletedEvent: | |
| ctx: RunContext | ||
| output: Any | Exception | None | ||
|
|
||
| def __init__(self, *, id: str) -> None: | ||
| def __init__(self, *, id: str, tools: list[Tool | Toolset] | None = None) -> None: | ||
| self._id = id | ||
| self._tools: Sequence[Tool | Toolset] = tools or [] | ||
|
|
||
| @property | ||
| def id(self) -> str: | ||
| return self._id | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def tools(self) -> list[Tool]: ... | ||
| def tools(self) -> Sequence[Tool | Toolset]: | ||
| return self._tools | ||
|
|
||
| async def setup(self) -> Self: | ||
| """Initialize the toolset and any nested toolsets. | ||
|
|
||
| Called automatically by ``AgentActivity`` when an agent starts. | ||
| """ | ||
| toolsets = [tool for tool in self.tools if isinstance(tool, Toolset)] | ||
| if toolsets: | ||
| await asyncio.gather(*(toolset.setup() for toolset in toolsets)) | ||
| return self | ||
|
|
||
| async def aclose(self) -> None: | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
| """Close the toolset and release any held resources. | ||
|
|
||
| Agent-scoped toolsets (passed to ``Agent(tools=...)``) are closed when the | ||
| ``AgentActivity`` ends (on agent transition or session close). Session-scoped | ||
| toolsets (passed to ``AgentSession(tools=...)``) are closed only when the | ||
| ``AgentSession`` shuts down. | ||
| """ | ||
| toolsets = [tool for tool in self.tools if isinstance(tool, Toolset)] | ||
| if toolsets: | ||
| await asyncio.gather(*(toolset.aclose() for toolset in toolsets)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add return_exceptions=True for both gather calls here? I guess fail fast is good too. |
||
|
|
||
|
|
||
| # Used by ToolChoice | ||
|
|
||
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.