99import mcp .server .stdio
1010
1111from .client import MeilisearchClient
12+ from .chat import ChatManager
1213from .logging import MCPLogger
1314
1415logger = MCPLogger ()
@@ -47,6 +48,7 @@ def __init__(
4748 self .url = url
4849 self .api_key = api_key
4950 self .meili_client = MeilisearchClient (url , api_key )
51+ self .chat_manager = ChatManager (self .meili_client .client )
5052 self .server = Server ("meilisearch" )
5153 self ._setup_handlers ()
5254
@@ -60,6 +62,7 @@ def update_connection(
6062 self .api_key = api_key
6163
6264 self .meili_client = MeilisearchClient (self .url , self .api_key )
65+ self .chat_manager = ChatManager (self .meili_client .client )
6366 self .logger .info ("Updated Meilisearch connection settings" , url = self .url )
6467
6568 def _setup_handlers (self ):
@@ -361,6 +364,99 @@ async def handle_list_tools() -> list[types.Tool]:
361364 "additionalProperties" : False ,
362365 },
363366 ),
367+ types .Tool (
368+ name = "create-chat-completion" ,
369+ description = "Create a conversational chat completion using Meilisearch's chat feature" ,
370+ inputSchema = {
371+ "type" : "object" ,
372+ "properties" : {
373+ "workspace_uid" : {
374+ "type" : "string" ,
375+ "description" : "Unique identifier of the chat workspace" ,
376+ },
377+ "messages" : {
378+ "type" : "array" ,
379+ "items" : {
380+ "type" : "object" ,
381+ "properties" : {
382+ "role" : {
383+ "type" : "string" ,
384+ "enum" : ["user" , "assistant" , "system" ],
385+ },
386+ "content" : {"type" : "string" },
387+ },
388+ "required" : ["role" , "content" ],
389+ },
390+ "description" : "List of message objects comprising the chat history" ,
391+ },
392+ "model" : {
393+ "type" : "string" ,
394+ "default" : "gpt-3.5-turbo" ,
395+ "description" : "The model to use for completion" ,
396+ },
397+ "stream" : {
398+ "type" : "boolean" ,
399+ "default" : True ,
400+ "description" : "Whether to stream the response (currently must be true)" ,
401+ },
402+ },
403+ "required" : ["workspace_uid" , "messages" ],
404+ "additionalProperties" : False ,
405+ },
406+ ),
407+ types .Tool (
408+ name = "get-chat-workspaces" ,
409+ description = "Get list of available chat workspaces" ,
410+ inputSchema = {
411+ "type" : "object" ,
412+ "properties" : {
413+ "offset" : {
414+ "type" : "integer" ,
415+ "description" : "Number of workspaces to skip" ,
416+ },
417+ "limit" : {
418+ "type" : "integer" ,
419+ "description" : "Maximum number of workspaces to return" ,
420+ },
421+ },
422+ "additionalProperties" : False ,
423+ },
424+ ),
425+ types .Tool (
426+ name = "get-chat-workspace-settings" ,
427+ description = "Get settings for a specific chat workspace" ,
428+ inputSchema = {
429+ "type" : "object" ,
430+ "properties" : {
431+ "workspace_uid" : {
432+ "type" : "string" ,
433+ "description" : "Unique identifier of the chat workspace" ,
434+ },
435+ },
436+ "required" : ["workspace_uid" ],
437+ "additionalProperties" : False ,
438+ },
439+ ),
440+ types .Tool (
441+ name = "update-chat-workspace-settings" ,
442+ description = "Update settings for a specific chat workspace" ,
443+ inputSchema = {
444+ "type" : "object" ,
445+ "properties" : {
446+ "workspace_uid" : {
447+ "type" : "string" ,
448+ "description" : "Unique identifier of the chat workspace" ,
449+ },
450+ "settings" : {
451+ "type" : "object" ,
452+ "description" : "Settings to update for the workspace" ,
453+ "additionalProperties" : True ,
454+ },
455+ },
456+ "required" : ["workspace_uid" , "settings" ],
457+ "additionalProperties" : False ,
458+ },
459+ ),
364460 ]
365461
366462 @self .server .call_tool ()
@@ -611,6 +707,66 @@ async def handle_call_tool(
611707 )
612708 ]
613709
710+ elif name == "create-chat-completion" :
711+ response = await self .chat_manager .create_chat_completion (
712+ workspace_uid = arguments ["workspace_uid" ],
713+ messages = arguments ["messages" ],
714+ model = arguments .get ("model" , "gpt-3.5-turbo" ),
715+ stream = arguments .get ("stream" , True ),
716+ )
717+ return [
718+ types .TextContent (
719+ type = "text" ,
720+ text = f"Chat completion response:\n { response } " ,
721+ )
722+ ]
723+
724+ elif name == "get-chat-workspaces" :
725+ workspaces = await self .chat_manager .get_chat_workspaces (
726+ offset = arguments .get ("offset" ) if arguments else None ,
727+ limit = arguments .get ("limit" ) if arguments else None ,
728+ )
729+ formatted_json = json .dumps (
730+ workspaces , indent = 2 , default = json_serializer
731+ )
732+ return [
733+ types .TextContent (
734+ type = "text" ,
735+ text = f"Chat workspaces:\n { formatted_json } " ,
736+ )
737+ ]
738+
739+ elif name == "get-chat-workspace-settings" :
740+ settings = await self .chat_manager .get_chat_workspace_settings (
741+ workspace_uid = arguments ["workspace_uid" ]
742+ )
743+ formatted_json = json .dumps (
744+ settings , indent = 2 , default = json_serializer
745+ )
746+ return [
747+ types .TextContent (
748+ type = "text" ,
749+ text = f"Workspace settings for '{ arguments ['workspace_uid' ]} ':\n { formatted_json } " ,
750+ )
751+ ]
752+
753+ elif name == "update-chat-workspace-settings" :
754+ updated_settings = (
755+ await self .chat_manager .update_chat_workspace_settings (
756+ workspace_uid = arguments ["workspace_uid" ],
757+ settings = arguments ["settings" ],
758+ )
759+ )
760+ formatted_json = json .dumps (
761+ updated_settings , indent = 2 , default = json_serializer
762+ )
763+ return [
764+ types .TextContent (
765+ type = "text" ,
766+ text = f"Updated workspace settings for '{ arguments ['workspace_uid' ]} ':\n { formatted_json } " ,
767+ )
768+ ]
769+
614770 raise ValueError (f"Unknown tool: { name } " )
615771
616772 except Exception as e :
0 commit comments