mcp with mcp inspector

mcp inspector provide us with the necessary tool to invoke a mcp tools. Let's get back to some basic. 

Here is my server.py and my client.py

server.py



from mcp.server.mcpserver import MCPServer

mcp = MCPServer("hello-world")

@mcp.tool()
def say_hello(name: str = "world") -> str:
    """Greet someone by name."""
    return f"Hello, {name}! 👋 (from my first MCP server)"

@mcp.tool()
def add(a: float, b: float) -> float:
    """Add two numbers."""
    return a + b

if __name__ == "__main__":
    # stdio transport: the client launches this script and talks over stdin/stdout
    mcp.run(transport="stdio")



client.py



import asyncio, sys
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def main():
    params = StdioServerParameters(command=sys.executable, args=["server.py"])
    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            tools = await session.list_tools()
            print("tools:", [t.name for t in tools.tools])
            r = await session.call_tool("say_hello", {"name": "Ada"})
            print("say_hello ->", r.content[0].text)
            r = await session.call_tool("add", {"a": 2, "b": 3})
            print("add ->", r.content[0].text)

asyncio.run(main())

Using vscode

To use vscode to invoke our mcp, we need the mcp.json


{
  "servers": {
    "hello-world": {
      "type": "stdio",
      "command": "${workspaceFolder}/.venv/Scripts/python.exe",
      "args": [
        "${workspaceFolder}/server.py"
      ]
    }
  }
}

And this is the output



Using mcp inspector 

To use the mcp inspector, please run the following command :-

 npx @modelcontextprotocol/inspector .venv/Scripts/python server.py


And you have to get connected first:


And invoking one of our tools











Comments

Popular posts from this blog

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

android build issue with version compatibility with agp and hilt : An exception occurred applying plugin request [id: 'com.google.dagger.hilt.android', version: '2.52']