MCP – contd
MCP Inspector
- Refer Here for mcp inspector
- To run this we need to install nodejs and best way to install nodejs is nvm
- installing nvm
# windows
winget install -e --id CoreyButler.NVMforWindows
# mac
brew install nvm
#linux
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
nvm --version
nvm install lts
nvm use lts
node --version
- To run the model inspector execute the following command
npx @modelcontextprotocol/inspector
- close the inspector and kill the process
- Now navigate to the project which we developed yesterday
- activate virtual environment
- now run the command
mcp dev <filename>.py
Lets build calc mcp server
- Create a new folder calc_mcp
mkdir calc_mcp
cd calc_mcp
uv init .
uv add mcp[cli] httpx
- Add the following code in calc.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP(name="calc-mcp")
@mcp.tool()
def add(a: int|float, b: int|float) -> int|float:
"""Adds two numbers
Args:
a (int): number
b (int): number
Returns:
int: a + b
"""
return a + b
@mcp.tool()
def multiply(a: int|float, b: int|float) -> int|float:
"""Multiplies two numbers
Args:
a (int): number
b (int): number
Returns:
int: a * b
"""
return a * b
@mcp.tool()
def divide(a: int|float, b: int|float) -> float:
"""Divides two numbers
Args:
a (int): number
b (int): number
Returns:
float: a / b
"""
return a / b
@mcp.tool()
def subtract(a: int|float, b: int|float) -> int|float:
"""Subtracts two numbers
Args:
a (int): number
b (int): number
Returns:
int: a - b
"""
return a - b
if __name__ == "__main__":
mcp.run(transport="stdio")
mcp dev calc.py
