Redoing fastapi + fastmcp
- Watch classroom recording for steps
- To run mcp inspector directly
npx @modelcontextprotocol/inspector
-
Lets also install one more tool Refer Here
-
Refer Here to run the application
Topics on server
- Context (Notification)
- Client (Langchain with mcp tools)
- Sampling
- Elicitations
- Auth (basic, jwt and oauth2)
Context
- When mcp tools are executing we need notifications
- logs
- progress
-
Best practices, tools which use context are generally recommended to be async.
-
Run this code and execute using mcp inspector
from fastmcp import FastMCP, Context
import asyncio
mcp = FastMCP("Streamable Demo")
@mcp.tool()
def add(a: int, b: int):
"""Adds two numbers"""
return a + b
@mcp.tool()
def echo(message: str):
"""Echoes a string"""
return f"you said {message}"
@mcp.tool()
async def some_long_running(a: int, b: int, ctx: Context) -> int:
ctx.info("Starting long running task")
asyncio.sleep(0.1)
ctx.info("Finished long running task")
# ctx progress
ctx.report_progress(0.25, 1.0)
ctx.report_progress(0.5, 1.0)
ctx.report_progress(0.75,1.0)
ctx.report_progress(1.0,1.0)
return a * b
if __name__ == "__main__":
mcp.run(transport="http", host="0.0.0.0", port=8000)
- See if you are recieving notifications
