1111
1212from mcp .server import Server
1313from mcp .server .stdio import stdio_server
14- from mcp .types import Prompt , Resource , TextContent , Tool
14+ from mcp .types import (
15+ CallToolResult ,
16+ GetPromptResult ,
17+ Prompt ,
18+ PromptMessage ,
19+ ReadResourceResult ,
20+ Resource ,
21+ TextContent ,
22+ TextResourceContents ,
23+ Tool ,
24+ )
1525
1626from . import prompts , resources
1727from .tools .base import Tool as BaseTool
@@ -86,33 +96,31 @@ async def list_tools() -> list[Tool]:
8696
8797
8898@app .call_tool ()
89- async def call_tool (name : str , arguments : dict [str , Any ]) -> list [ TextContent ] :
99+ async def call_tool (name : str , arguments : dict [str , Any ]) -> CallToolResult :
90100 """执行工具调用。"""
91101 if name not in TOOLS :
92- return [
93- TextContent (
94- type = "text" ,
95- text = f"Unknown tool: { name } " ,
96- )
97- ]
102+ return CallToolResult (
103+ content = [TextContent (type = "text" , text = f"Unknown tool: { name } " )],
104+ isError = True ,
105+ )
98106
99107 tool = TOOLS [name ]
100108 try :
101109 result = await tool .execute (** arguments )
102- return [
103- TextContent (
104- type = "text" ,
105- text = str ( result . to_dict ()) ,
106- )
107- ]
110+ result_dict = result . to_dict ()
111+ return CallToolResult (
112+ content = [ TextContent ( type = "text" , text = str ( result_dict ))] ,
113+ structuredContent = result_dict ,
114+ isError = not result . success ,
115+ )
108116 except Exception as e :
109117 error_result = ToolResult .fail (str (e ))
110- return [
111- TextContent (
112- type = "text" ,
113- text = str ( error_result . to_dict ()) ,
114- )
115- ]
118+ error_dict = error_result . to_dict ()
119+ return CallToolResult (
120+ content = [ TextContent ( type = "text" , text = str ( error_dict ))] ,
121+ structuredContent = error_dict ,
122+ isError = True ,
123+ )
116124
117125
118126def main () -> None :
@@ -148,16 +156,41 @@ async def list_resources() -> list[Resource]:
148156
149157
150158@app .read_resource ()
151- async def read_resource (uri : str ) -> str :
159+ async def read_resource (uri : str ) -> ReadResourceResult :
152160 """读取资源内容。"""
153161 if uri .startswith ("template://" ):
154162 template_name = uri [11 :]
155163 path = resources .get_template_path (template_name )
156164 if path :
157165 with open (path , encoding = "utf-8" ) as f :
158- return f .read ()
159- return f"Template not found: { template_name } "
160- return f"Unknown resource: { uri } "
166+ content = f .read ()
167+ return ReadResourceResult (
168+ contents = [
169+ TextResourceContents (
170+ uri = uri ,
171+ text = content ,
172+ mimeType = "text/plain" ,
173+ )
174+ ]
175+ )
176+ return ReadResourceResult (
177+ contents = [
178+ TextResourceContents (
179+ uri = uri ,
180+ text = f"Template not found: { template_name } " ,
181+ mimeType = "text/plain" ,
182+ )
183+ ]
184+ )
185+ return ReadResourceResult (
186+ contents = [
187+ TextResourceContents (
188+ uri = uri ,
189+ text = f"Unknown resource: { uri } " ,
190+ mimeType = "text/plain" ,
191+ )
192+ ]
193+ )
161194
162195
163196@app .list_prompts ()
@@ -173,12 +206,28 @@ async def list_prompts() -> list[Prompt]:
173206
174207
175208@app .get_prompt ()
176- async def get_prompt (name : str , arguments : dict [str , str ] | None = None ) -> str :
209+ async def get_prompt (name : str , arguments : dict [str , str ] | None = None ) -> GetPromptResult :
177210 """获取提示词内容。"""
178211 content = prompts .get_prompt (name )
179212 if not content :
180- return f"Prompt not found: { name } "
181- return content
213+ return GetPromptResult (
214+ description = "Error: Prompt not found" ,
215+ messages = [
216+ PromptMessage (
217+ role = "user" ,
218+ content = TextContent (type = "text" , text = f"Prompt not found: { name } " ),
219+ )
220+ ],
221+ )
222+ return GetPromptResult (
223+ description = f"Prompt template: { name } " ,
224+ messages = [
225+ PromptMessage (
226+ role = "user" ,
227+ content = TextContent (type = "text" , text = content ),
228+ )
229+ ],
230+ )
182231
183232
184233if __name__ == "__main__" :
0 commit comments