-
Notifications
You must be signed in to change notification settings - Fork 387
Flexible MCPClient Initialization #705
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
suluyana
wants to merge
6
commits into
modelscope:main
Choose a base branch
from
suluyana:feat/add_mcp_client_input
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright (c) Alibaba, Inc. and its affiliates. | ||
import os | ||
import asyncio | ||
import unittest | ||
|
||
from ms_agent.tools.mcp_client import MCPClient | ||
|
||
|
||
from modelscope.utils.test_utils import test_level | ||
|
||
class TestMCPClient(unittest.TestCase): | ||
mcp_config = { | ||
"mcpServers": { | ||
"fetch": { | ||
"type": "sse", | ||
"url": os.getenv("MCP_SERVER_FETCH_URL"), | ||
} | ||
} | ||
} | ||
mcp_config2 = { | ||
"mcpServers": { | ||
"time": { | ||
"type": "sse", | ||
"url": os.getenv("MCP_SERVER_TIME_URL"), | ||
} | ||
} | ||
} | ||
|
||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') | ||
def test_outside_init(self): | ||
async def main(): | ||
async with MCPClient(self.mcp_config) as mcp_client: | ||
mcps = await mcp_client.get_tools() | ||
assert('fetch' in mcps) | ||
|
||
res = await mcp_client.call_tool(server_name='fetch', | ||
tool_name='fetch', | ||
tool_args={'url': 'http://www.baidu.com'}) | ||
assert('baidu' in res) | ||
asyncio.run(main()) | ||
|
||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') | ||
def test_aenter(self): | ||
async def main(): | ||
mcp_client = MCPClient(self.mcp_config) | ||
await mcp_client.__aenter__() | ||
mcps = await mcp_client.get_tools() | ||
assert('fetch' in mcps) | ||
await mcp_client.__aexit__(None, None, None) | ||
|
||
asyncio.run(main()) | ||
|
||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') | ||
def test_normal_connect(self): | ||
async def main(): | ||
mcp_client = MCPClient(self.mcp_config) | ||
await mcp_client.connect() | ||
mcps = await mcp_client.get_tools() | ||
assert('fetch' in mcps) | ||
await mcp_client.cleanup() | ||
|
||
asyncio.run(main()) | ||
|
||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') | ||
def test_add_config(self): | ||
async def main(): | ||
async with MCPClient(self.mcp_config) as mcp_client: | ||
await mcp_client.add_mcp_config(self.mcp_config2) | ||
mcps = await mcp_client.get_tools() | ||
assert ('fetch' in mcps and 'time' in mcps) | ||
|
||
asyncio.run(main()) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
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.
Replacing
await self.servers.cleanup()
withself.servers = None
introduces a resource leak. If theToolManager
creates its ownMCPClient
instance (in theelse
branch of theconnect
method), it is responsible for cleaning it up. The current implementation fails to do so.The cleanup should be conditional. You could track if the client is managed internally. For example:
In
__init__
:In
cleanup
: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.
fixed