62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import asyncio
|
|
import os
|
|
import sys
|
|
from mcp import ClientSession, StdioServerParameters
|
|
from mcp.client.stdio import stdio_client
|
|
from google import genai
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
async def main():
|
|
print("Starting MCP Client...")
|
|
|
|
# Check for API Key
|
|
api_key = os.getenv("GOOGLE_API_KEY")
|
|
if not api_key:
|
|
print("Warning: GOOGLE_API_KEY is not set in .env. Gemini calls will fail.")
|
|
else:
|
|
print("GOOGLE_API_KEY found.")
|
|
|
|
# Define the server parameters
|
|
# We run the simple_server.py we created earlier
|
|
server_script = os.path.join(os.path.dirname(__file__), "simple_server.py")
|
|
|
|
server_params = StdioServerParameters(
|
|
command=sys.executable, # Use the current python interpreter
|
|
args=[server_script],
|
|
env=None
|
|
)
|
|
|
|
print(f"Connecting to MCP server at: {server_script}")
|
|
|
|
try:
|
|
async with stdio_client(server_params) as (read, write):
|
|
async with ClientSession(read, write) as session:
|
|
# Initialize the session
|
|
await session.initialize()
|
|
|
|
# List available tools
|
|
result = await session.list_tools()
|
|
tools = result.tools
|
|
|
|
print(f"\nSuccessfully connected! Found {len(tools)} tools:")
|
|
for tool in tools:
|
|
print(f" - {tool.name}: {tool.description}")
|
|
print(f" Schema: {tool.inputSchema}")
|
|
|
|
print("\n--- Integration with Gemini (google-genai) ---")
|
|
print("To integrate these tools with Gemini using the new SDK, you would:")
|
|
print("1. Convert the MCP tool schemas to Gemini tool definitions.")
|
|
print("2. Initialize the Gemini client:")
|
|
print(" from google import genai")
|
|
print(" client = genai.Client(api_key=api_key)")
|
|
print("3. Pass the tools to the generate_content call.")
|
|
print("4. Handle the tool calls returned by the model.")
|
|
|
|
except Exception as e:
|
|
print(f"Error connecting to MCP server: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |