1212 ChatEventType ,
1313 Voice ,
1414 User ,
15+ Workspace ,
1516)
1617from mcp .server import FastMCP # type: ignore
1718from pydantic import BaseModel # type: ignore
@@ -79,85 +80,110 @@ async def workflow_chat(self, bot_id: str, workflow_id: str, content: str) -> st
7980server = CozeServer (conf .api_base , conf .api_token )
8081
8182
83+ def wrap_id (model : Union [BaseModel , List [BaseModel ]], field : str = "id" ):
84+ if isinstance (model , list ):
85+ res = []
86+ for item in model :
87+ v = item .model_dump ()
88+ v [field ] = "id:" + v [field ]
89+ res .append (v )
90+ return res
91+
92+ v = model .model_dump ()
93+ v [field ] = "id:" + v [field ]
94+ return v
95+
96+
97+ def unwrap_id (value : str ) -> str :
98+ if value and value .startswith ("id:" ):
99+ return str (value [3 :])
100+ return str (value )
101+
102+
82103@mcp .tool (description = "get self user info" )
83104async def get_me () -> User :
84105 return await server .coze .users .me ()
85106
86107
87108@mcp .tool (description = "list coze workspaces" )
88- async def list_workspaces ():
109+ async def list_workspaces () -> List [ Workspace ] :
89110 res = await server .coze .workspaces .list ()
90- return [item async for item in res ]
111+ items = [item async for item in res ]
112+ return wrap_id (items ) # type: ignore
91113
92114
93115@mcp .tool (description = "list bots in workspaces" )
94- async def list_bots (workspace_id : Union [int , str ]):
95- res = await server .coze .bots .list (space_id = str (workspace_id ))
96- return res .items
116+ async def list_bots (workspace_id : str ) -> List [Bot ]:
117+ res = await server .coze .bots .list (space_id = unwrap_id (workspace_id ))
118+ items = [item async for item in res ]
119+ return wrap_id (items , "bot_id" ) # type: ignore
97120
98121
99122@mcp .tool (description = "retrieve bot" )
100- async def retrieve_bot (bot_id : Union [int , str ]) -> Bot :
101- return await server .coze .bots .retrieve (bot_id = str (bot_id ))
123+ async def retrieve_bot (bot_id : str ) -> Bot :
124+ bot = await server .coze .bots .retrieve (bot_id = unwrap_id (bot_id ))
125+ return wrap_id (bot , "bot_id" ) # type: ignore
102126
103127
104128@mcp .tool (description = "create bot in workspaces" )
105129async def create_bot (
106- workspace_id : Union [ int , str ] ,
130+ workspace_id : str ,
107131 name : str ,
108132 description : Optional [str ] = None ,
109133 prompt : Optional [str ] = None ,
110134) -> Bot :
111- return await server .coze .bots .create (
112- space_id = str (workspace_id ),
135+ bot = await server .coze .bots .create (
136+ space_id = unwrap_id (workspace_id ),
113137 name = name ,
114138 description = description ,
115139 prompt_info = None if not prompt else BotPromptInfo (prompt = prompt ),
116140 )
141+ return wrap_id (bot , "bot_id" ) # type: ignore
117142
118143
119144@mcp .tool (description = "update bot info" )
120145async def update_bot (
121- bot_id : Union [ int , str ] ,
146+ bot_id : str ,
122147 name : Optional [str ] = None ,
123148 description : Optional [str ] = None ,
124149 prompt : Optional [str ] = None ,
125150):
126151 await server .coze .bots .update (
127- bot_id = str (bot_id ),
152+ bot_id = unwrap_id (bot_id ),
128153 name = name ,
129154 description = description ,
130155 prompt_info = None if not prompt else BotPromptInfo (prompt = prompt ),
131156 )
132157
133158
134159@mcp .tool (description = "publish bot info" )
135- async def publish_bot (bot_id : Union [ int , str ] ) -> Bot :
136- return await server .coze .bots .publish (
137- bot_id = str (bot_id ),
160+ async def publish_bot (bot_id : str ) -> Bot :
161+ bot = await server .coze .bots .publish (
162+ bot_id = unwrap_id (bot_id ),
138163 )
164+ return wrap_id (bot , "bot_id" ) # type: ignore
139165
140166
141167@mcp .tool (description = "chat with bot" )
142168async def chat_with_bot (
143- bot_id : Union [ int , str ] ,
169+ bot_id : str ,
144170 content : str ,
145171) -> str :
146172 return await server .bot_chat (
147- bot_id = str (bot_id ),
173+ bot_id = unwrap_id (bot_id ),
148174 content = content ,
149175 )
150176
151177
152178@mcp .tool (description = "chat with bot" )
153179async def chat_with_workflow (
154- bot_id : Union [ int , str ] ,
155- workflow_id : Union [ int , str ] ,
180+ bot_id : str ,
181+ workflow_id : str ,
156182 content : str ,
157183) -> str :
158184 return await server .workflow_chat (
159- bot_id = str (bot_id ),
160- workflow_id = str (workflow_id ),
185+ bot_id = unwrap_id (bot_id ),
186+ workflow_id = unwrap_id (workflow_id ),
161187 content = content ,
162188 )
163189
@@ -166,4 +192,4 @@ async def chat_with_workflow(
166192async def list_voices () -> List [Voice ]:
167193 res = await server .coze .audio .voices .list ()
168194 items = [item async for item in res ]
169- return items
195+ return wrap_id ( items , "voice_id" ) # type: ignore
0 commit comments