22import json
33import os
44from typing import Optional , Dict , Any , List , Union
5+ from datetime import datetime
56import mcp .types as types
67from mcp .server import Server , NotificationOptions
78from mcp .server .models import InitializationOptions
1314logger = MCPLogger ()
1415
1516
17+ def json_serializer (obj : Any ) -> str :
18+ """Custom JSON serializer for objects not serializable by default json code"""
19+ if isinstance (obj , datetime ):
20+ return obj .isoformat ()
21+ return str (obj )
22+
23+
1624def create_server (url : str = "http://localhost:7700" , api_key : Optional [str ] = None ) -> "MeilisearchMCPServer" :
1725 """Create and return a configured MeilisearchMCPServer instance"""
1826 return MeilisearchMCPServer (url , api_key )
@@ -243,7 +251,7 @@ async def handle_call_tool(
243251
244252 elif name == "list-indexes" :
245253 indexes = await self .meili_client .get_indexes ()
246- formatted_json = json .dumps (indexes , indent = 2 )
254+ formatted_json = json .dumps (indexes , indent = 2 , default = json_serializer )
247255 return [
248256 types .TextContent (
249257 type = "text" , text = f"Indexes:\n { formatted_json } "
@@ -386,7 +394,7 @@ async def handle_call_tool(
386394 return [
387395 types .TextContent (
388396 type = "text" ,
389- text = f"Health status: { json .dumps (status .__dict__ , default = str )} " ,
397+ text = f"Health status: { json .dumps (status .__dict__ , default = json_serializer )} " ,
390398 )
391399 ]
392400
@@ -401,7 +409,7 @@ async def handle_call_tool(
401409 )
402410 return [
403411 types .TextContent (
404- type = "text" , text = f"Index metrics: { metrics .__dict__ } "
412+ type = "text" , text = f"Index metrics: { json . dumps ( metrics .__dict__ , default = json_serializer ) } "
405413 )
406414 ]
407415
0 commit comments