-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sanitize function name for langchain tools (#7)
* add pytest asyncio with setup * add a basic async tests for langchain * add sync version test as well * add tests for keyword toolname and dashes in tool name * add sanitizing function and passing test
- Loading branch information
Showing
4 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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,103 @@ | ||
from textwrap import dedent | ||
|
||
import pytest | ||
from mcp import StdioServerParameters | ||
|
||
from mcpadapt.core import MCPAdapt | ||
from mcpadapt.langchain_adapter import LangChainAdapter | ||
|
||
|
||
@pytest.fixture | ||
def echo_server_script(): | ||
return dedent( | ||
''' | ||
from mcp.server.fastmcp import FastMCP | ||
mcp = FastMCP("Echo Server") | ||
@mcp.tool() | ||
def echo_tool(text: str) -> str: | ||
"""Echo the input text""" | ||
return f"Echo: {text}" | ||
mcp.run() | ||
''' | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_basic_async(echo_server_script): | ||
async with MCPAdapt( | ||
StdioServerParameters( | ||
command="uv", args=["run", "python", "-c", echo_server_script] | ||
), | ||
LangChainAdapter(), | ||
) as tools: | ||
assert len(tools) == 1 # we expect one tool as defined above | ||
assert tools[0].name == "echo_tool" # we expect the tool to be named echo_tool | ||
response = await tools[0].ainvoke("hello") | ||
assert response == "Echo: hello" # we expect the tool to return "Echo: hello" | ||
|
||
|
||
def test_basic_sync(echo_server_script): | ||
with MCPAdapt( | ||
StdioServerParameters( | ||
command="uv", args=["run", "python", "-c", echo_server_script] | ||
), | ||
LangChainAdapter(), | ||
) as tools: | ||
assert len(tools) == 1 | ||
assert tools[0].name == "echo_tool" | ||
assert tools[0].invoke("hello") == "Echo: hello" | ||
|
||
|
||
def test_tool_name_with_dashes(): | ||
mcp_server_script = dedent( | ||
''' | ||
from mcp.server.fastmcp import FastMCP | ||
mcp = FastMCP("Echo Server") | ||
@mcp.tool(name="echo-tool") | ||
def echo_tool(text: str) -> str: | ||
"""Echo the input text""" | ||
return f"Echo: {text}" | ||
mcp.run() | ||
''' | ||
) | ||
with MCPAdapt( | ||
StdioServerParameters( | ||
command="uv", args=["run", "python", "-c", mcp_server_script] | ||
), | ||
LangChainAdapter(), | ||
) as tools: | ||
assert len(tools) == 1 | ||
assert tools[0].name == "echo_tool" | ||
assert tools[0].invoke("hello") == "Echo: hello" | ||
|
||
|
||
def test_tool_name_with_keyword(): | ||
mcp_server_script = dedent( | ||
''' | ||
from mcp.server.fastmcp import FastMCP | ||
mcp = FastMCP("Echo Server") | ||
@mcp.tool(name="def") | ||
def echo_tool(text: str) -> str: | ||
"""Echo the input text""" | ||
return f"Echo: {text}" | ||
mcp.run() | ||
''' | ||
) | ||
with MCPAdapt( | ||
StdioServerParameters( | ||
command="uv", args=["run", "python", "-c", mcp_server_script] | ||
), | ||
LangChainAdapter(), | ||
) as tools: | ||
assert len(tools) == 1 | ||
assert tools[0].name == "def_" | ||
assert tools[0].invoke("hello") == "Echo: hello" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.