Skip to content

Commit a2d51e4

Browse files
fennb0Delta
authored andcommitted
feat: add resource annotations support to FastMCP (modelcontextprotocol#1468)
1 parent 8ce0cf9 commit a2d51e4

File tree

1 file changed

+9
-1
lines changed
  • src/mcp/server/fastmcp/resources

1 file changed

+9
-1
lines changed

src/mcp/server/fastmcp/resources/types.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ async def read(self, context: Any | None = None) -> str | bytes:
6666
else:
6767
result = self.fn(**args)
6868

69+
# Support cases where a sync function returns a coroutine
70+
if inspect.iscoroutine(result):
71+
result = await result
72+
73+
# Support returning a Resource instance (recursive read)
74+
if isinstance(result, Resource):
75+
return await result.read(context)
76+
6977
if isinstance(result, str | bytes):
7078
return result
7179
if isinstance(result, pydantic.BaseModel):
@@ -208,4 +216,4 @@ async def read(self, context: Any | None = None) -> str: # Always returns JSON
208216
file_list = [str(f.relative_to(self.path)) for f in files if f.is_file()]
209217
return json.dumps({"files": file_list}, indent=2)
210218
except Exception as e:
211-
raise ValueError(f"Error reading directory {self.path}: {e}")
219+
raise ValueError(f"Error reading directory {self.path}: {e}")

0 commit comments

Comments
 (0)