mcp tool use

This commit is contained in:
zachary62 2025-03-29 11:46:37 -04:00
parent 558808f6db
commit a6a34d2f14
2 changed files with 73 additions and 44 deletions

View File

@ -1,44 +0,0 @@
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
# Set up connection to your server
server_params = StdioServerParameters(
command="python",
args=["simple_server.py"]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()
# List available tools
tools_response = await session.list_tools()
# Extract tools information
tools = tools_response.tools
# Parse each tool
for tool in tools:
print("\nTool Information:")
print(f" Name: {tool.name}")
print(f" Description: {tool.description}")
print(f" Required Parameters: {tool.inputSchema.get('required', [])}")
# Parse parameter information
properties = tool.inputSchema.get('properties', {})
print(" Parameters:")
for param_name, param_info in properties.items():
param_type = param_info.get('type', 'unknown')
param_title = param_info.get('title', param_name)
print(f" - {param_name} ({param_type}): {param_title}")
# Call the add tool
result = await session.call_tool("add", {"a": 5, "b": 3})
result_value = result.content[0].text
print(f"5 + 3 = {result_value}")
if __name__ == "__main__":
asyncio.run(main())

View File

@ -1,5 +1,8 @@
from openai import OpenAI
import os
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
def call_llm(messages):
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
@ -12,6 +15,40 @@ def call_llm(messages):
return response.choices[0].message.content
def get_tools(server_script_path):
"""Get available tools from an MCP server.
"""
async def _get_tools():
server_params = StdioServerParameters(
command="python",
args=[server_script_path]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools_response = await session.list_tools()
return tools_response.tools
return asyncio.run(_get_tools())
def call_tool(server_script_path, tool_name, arguments):
"""Call a tool on an MCP server.
"""
async def _call_tool():
server_params = StdioServerParameters(
command="python",
args=[server_script_path]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool(tool_name, arguments)
return result.content[0].text
return asyncio.run(_call_tool())
if __name__ == "__main__":
# Test the LLM call
messages = [{"role": "user", "content": "In a few words, what's the meaning of life?"}]
@ -19,3 +56,39 @@ if __name__ == "__main__":
print(f"Prompt: {messages[0]['content']}")
print(f"Response: {response}")
# Find available tools
print("=== Finding available tools ===")
tools = get_tools("simple_server.py")
# Print tool information nicely formatted
for i, tool in enumerate(tools, 1):
print(f"\nTool {i}: {tool.name}")
print("=" * (len(tool.name) + 8))
print(f"Description: {tool.description}")
# Parameters section
print("Parameters:")
properties = tool.inputSchema.get('properties', {})
required = tool.inputSchema.get('required', [])
# No parameters case
if not properties:
print(" None")
# Print each parameter with its details
for param_name, param_info in properties.items():
param_type = param_info.get('type', 'unknown')
req_status = "(Required)" if param_name in required else "(Optional)"
print(f"{param_name}: {param_type} {req_status}")
# Call a tool
print("\n=== Calling the add tool ===")
a, b = 5, 3
result = call_tool("simple_server.py", "add", {"a": a, "b": b})
print(f"Result of {a} + {b} = {result}")
# You can easily call with different parameters
a, b = 10, 20
result = call_tool("simple_server.py", "add", {"a": a, "b": b})
print(f"Result of {a} + {b} = {result}")