MCP Learning Series - Part 2
In the previous post we say what is MCP? In this post, we will create an MCP server and add it to Claude Desktop.
Claude Desktop is our AI application (MCP Host) which has inbuild MCP Client to connect to our MCP Server.
The MCP server which I am going to create is a simple math tool which can perform basic mathematical operations.
Simple Code for creating Mathematics Operations:
We use the decorator @mcp.tool to create a MCP tool. MCP allows to create Tools, Resources and Prompts.
# server.py
from mcp.server.mcpserver import MCPServer
mcp = MCPServer("Math Server")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers together."""
return a + b
def sub(a: int, b:int) -> int:
"""Sub two numbers"""
return a - b
def multiply(a: int, b:int) -> int:
"""Multiple two numbers"""
return a * b
def divide(a:int, b:int) -> int:
"""Divide two numbers"""
return a / b
if __name__ == "__main__":
mcp.run()
Start the MCP server using the command $ mcp dev math_server.py
Why we are seeing just add tool alone? Not the other ones?
In the Python MCP SDK:
Only functions decorated with
@mcp.tool()become toolsUndecorated functions (like your
sub,multiply,divide) remain normal Python functions and are not exposed to the MCP client
So the server currently exposes exactly one tool.
# server.py
from mcp.server import MCPServer
mcp = MCPServer("MathsMCP")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers together."""
return a + b
@mcp.tool()
def sub(a: int, b:int) -> int:
"""Sub two numbers"""
return a - b
@mcp.tool()
def multiply(a: int, b:int) -> int:
"""Multiple two numbers"""
return a * b
@mcp.tool()
def divide(a:int, b:int) -> int:
"""Divide two numbers"""
return a / b
if __name__ == "__main__":
mcp.run()








Comments
Post a Comment