Elicitation with dataclasses #1754
Unanswered
yongzheJIN
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
fastmcp : 2.10.6
I could not use dataclasses to reply the question from mcp server. But from the offcial document:
"
response_type: A Python dataclass type that FastMCP created from the server’s JSON schema. Use this to construct your response with proper typing and IDE support. If the server requests an empty object (indicating no response), this will be None.
"
-------------------------------------------mcp server code:
"
@app.tool("get_elderly_name_by_ID")
async def get_elderly_name_by_id(elderly_id: str, ctx: Context) -> str:
"""
get elderly name by id
"""
print(elderly_id)
if not elderly_id or elderly_id == " ":
res = await ctx.elicit("supply the id card number:", response_type=ElderlyIDArgs)
print(res)
if hasattr(res, "data"):
elderly_id = res.data.elderly_id
else:
return f"{res.class.name}。"
# TODO: 在这里查询数据库;演示直接返回固定值
return "111"
"
---------------------------------------------------------------elication_handler:
"
async def server_handler(self, message: str, response_type: type, params: ElicitRequestParams, context: Context):
"""
"""
tool_invoke_message = McpToolInvokeMessage(
message_type="elicitation",
message_id=self.process_id,
answer=params.requestedSchema,
message_title=params.message
)
await self.queue.put((tool_invoke_message, self.process_id))
# bind waiting list
fut = asyncio.get_running_loop().create_future()
self.waiting[self.process_id] = fut
answer = await self.waiting[self.process_id] # 阻塞在这里
self.queue.task_done()
self.waiting.pop(self.process_id, None)
# constructe server
if dataclasses.is_dataclass(response_type):
response_obj = dataclasses.asdict(response_type(**answer))
print(response_obj)
else:
response_obj = answer
print('response_obj---', response_obj)
return ElicitResult(action="accept", content=response_obj)
"
-------------------------------------the error as show as below:
"
fastmcp.exceptions.ToolError: Error calling tool 'get_elderly_name_by_ID': 4 validation errors for ElicitResult
content.content.str
Input should be a valid string [type=string_type, input_value={'elderly_id': '331082'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/string_type
content.content.int
Input should be a valid integer [type=int_type, input_value={'elderly_id': '331082'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/int_type
content.content.float
Input should be a valid number [type=float_type, input_value={'elderly_id': '331082'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/float_type
content.content.bool
Input should be a valid boolean [type=bool_type, input_value={'elderly_id': '331082'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/bool_type
"
pls anyone knows how to pass dataclasses in communication between mcp client and server.
Beta Was this translation helpful? Give feedback.
All reactions