पाठ 11 / 25
आपका पहला Server और Tool
आधिकारिक Python SDK से server बनाएँ और typed function को tool के रूप में उपलब्ध कराएँ।
कुछ पंक्तियों में FastMCP
आधिकारिक Python SDK में FastMCP है। आप server object बनाते हैं और सामान्य functions को @mcp.tool() से decorate करते हैं। SDK आपके type hints से JSON schema बनाता है और docstring को description की तरह उपयोग करता है, इसलिए अच्छे types और docstrings ही आपके tool का दस्तावेज़ हैं।
Function से server तक
सादे functions को decorate करें, transport चुनें, server चलाएँ और inspector से जाँचें।
Calculator server
pip install "mcp[cli]" से install करें। डिफ़ॉल्ट transport stdio है, इसलिए host इस script को subprocess के रूप में शुरू करता है।
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers and return the sum."""
return a + b
if __name__ == "__main__":
mcp.run() # stdio transport by defaultstdio पर stdout में print न करें
stdio transport में standard output protocol messages ले जाता है। अकेला print() stream बिगाड़ देता है। इसके बजाय standard error या file में log करें।
त्वरित जाँच: FastMCP tool का input schema कहाँ से पाता है?
- Function के type hints से
- अंदाज़े से
- फ़ाइल के नाम से
- Operating system से
Answer
Function के type hints से — Type hints parameter types तय करते हैं और docstring description बन जाती है।