Skip to content

Commit eb5cb84

Browse files
pamelafoxCopilot
andauthored
Upgrade to FastMCP 3.0, update get/set state to async (#34)
* Upgrade to FastMCP 3.0, update get/set state * Ignore logging warnings * Update pyproject.toml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 44c5f19 commit eb5cb84

File tree

4 files changed

+31
-152
lines changed

4 files changed

+31
-152
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Demonstration of Python FastMCP servers"
55
readme = "README.md"
66
requires-python = "==3.13.*"
77
dependencies = [
8-
"fastmcp>=2.14.2",
8+
"fastmcp>=3.0.0b1",
99
"debugpy>=1.8.0",
1010
"langchain-core>=0.3.0",
1111
"mcp>=1.3.0",

servers/auth_entra_mcp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ def _get_user_id(self):
144144
async def on_call_tool(self, context: MiddlewareContext, call_next):
145145
user_id = self._get_user_id()
146146
if context.fastmcp_context is not None:
147-
context.fastmcp_context.set_state("user_id", user_id)
147+
await context.fastmcp_context.set_state("user_id", user_id)
148148
return await call_next(context)
149149

150150
async def on_read_resource(self, context: MiddlewareContext, call_next):
151151
user_id = self._get_user_id()
152152
if context.fastmcp_context is not None:
153-
context.fastmcp_context.set_state("user_id", user_id)
153+
await context.fastmcp_context.set_state("user_id", user_id)
154154
return await call_next(context)
155155

156156

@@ -191,7 +191,7 @@ async def add_user_expense(
191191

192192
try:
193193
# Read user_id stored by middleware
194-
user_id = ctx.get_state("user_id")
194+
user_id = await ctx.get_state("user_id")
195195
if not user_id:
196196
return "Error: Authentication required (no user_id present)"
197197
expense_id = str(uuid.uuid4())
@@ -217,7 +217,7 @@ async def get_user_expenses(ctx: Context):
217217
"""Get the authenticated user's expense data from Cosmos DB."""
218218

219219
try:
220-
user_id = ctx.get_state("user_id")
220+
user_id = await ctx.get_state("user_id")
221221
if not user_id:
222222
return "Error: Authentication required (no user_id present)"
223223
query = "SELECT * FROM c WHERE c.user_id = @uid ORDER BY c.date DESC"

servers/auth_keycloak_mcp.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import os
99
import uuid
10+
import warnings
1011
from datetime import date
1112
from enum import Enum
1213
from typing import Annotated
@@ -35,7 +36,7 @@
3536

3637
logging.basicConfig(
3738
level=logging.WARNING,
38-
format="%(message)s",
39+
format="%(name)s: %(message)s",
3940
handlers=[
4041
RichHandler(
4142
console=Console(stderr=True),
@@ -45,6 +46,9 @@
4546
)
4647
],
4748
)
49+
# Suppress OTEL 1.39 deprecation warnings and noisy logs
50+
warnings.filterwarnings("ignore", category=DeprecationWarning, message=r".*Deprecated since version 1\.39\.0.*")
51+
logging.getLogger("azure.monitor.opentelemetry.exporter._performance_counters._manager").setLevel(logging.ERROR)
4852
logger = logging.getLogger("ExpensesMCP")
4953
logger.setLevel(logging.INFO)
5054

@@ -108,13 +112,13 @@ def _get_user_id(self):
108112
async def on_call_tool(self, context: MiddlewareContext, call_next):
109113
user_id = self._get_user_id()
110114
if context.fastmcp_context is not None:
111-
context.fastmcp_context.set_state("user_id", user_id)
115+
await context.fastmcp_context.set_state("user_id", user_id)
112116
return await call_next(context)
113117

114118
async def on_read_resource(self, context: MiddlewareContext, call_next):
115119
user_id = self._get_user_id()
116120
if context.fastmcp_context is not None:
117-
context.fastmcp_context.set_state("user_id", user_id)
121+
await context.fastmcp_context.set_state("user_id", user_id)
118122
return await call_next(context)
119123

120124

@@ -155,7 +159,7 @@ async def add_user_expense(
155159

156160
try:
157161
# Read user_id stored by middleware
158-
user_id = ctx.get_state("user_id")
162+
user_id = await ctx.get_state("user_id")
159163
if not user_id:
160164
return "Error: Authentication required (no user_id present)"
161165
expense_id = str(uuid.uuid4())
@@ -181,7 +185,7 @@ async def get_user_expenses(ctx: Context):
181185
"""Get the authenticated user's expense data from Cosmos DB."""
182186

183187
try:
184-
user_id = ctx.get_state("user_id")
188+
user_id = await ctx.get_state("user_id")
185189
if not user_id:
186190
return "Error: Authentication required (no user_id present)"
187191
query = "SELECT * FROM c WHERE c.user_id = @uid ORDER BY c.date DESC"

0 commit comments

Comments
 (0)