Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/meilisearch_mcp/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,43 @@ async def get_documents(
"""Get documents from an index"""
try:
index = self.client.index(index_uid)
return index.get_documents(
{"offset": offset, "limit": limit, "fields": fields}
)
# Build parameters dict, excluding None values to avoid API errors
params = {}
if offset is not None:
params["offset"] = offset
if limit is not None:
params["limit"] = limit
if fields is not None:
params["fields"] = fields

result = index.get_documents(params if params else {})

# Convert meilisearch model objects to JSON-serializable format
if hasattr(result, '__dict__'):
result_dict = result.__dict__.copy()
# Convert individual document objects in results if they exist
if 'results' in result_dict and isinstance(result_dict['results'], list):
serialized_results = []
for doc in result_dict['results']:
if hasattr(doc, '__dict__'):
# Extract the actual document data
doc_dict = doc.__dict__.copy()
# Look for private attributes that might contain the actual data
for key, value in doc_dict.items():
if key.startswith('_') and isinstance(value, dict):
# Use the dict content instead of the wrapper
serialized_results.append(value)
break
else:
# If no private dict found, use the object dict directly
serialized_results.append(doc_dict)
else:
serialized_results.append(doc)
result_dict['results'] = serialized_results
return result_dict
else:
return result

except Exception as e:
raise Exception(f"Failed to get documents: {str(e)}")

Expand Down
15 changes: 12 additions & 3 deletions src/meilisearch_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def json_serializer(obj: Any) -> str:
"""Custom JSON serializer for objects not serializable by default json code"""
if isinstance(obj, datetime):
return obj.isoformat()
# Handle Meilisearch model objects by using their __dict__ if available
if hasattr(obj, '__dict__'):
return obj.__dict__
return str(obj)


Expand Down Expand Up @@ -318,13 +321,19 @@ async def handle_call_tool(
]

elif name == "get-documents":
# Use default values to fix issue #17 (None offset/limit causes API errors)
offset = arguments.get("offset", 0)
limit = arguments.get("limit", 20)
documents = await self.meili_client.documents.get_documents(
arguments["indexUid"],
arguments.get("offset"),
arguments.get("limit"),
offset,
limit,
)
# Convert DocumentsResults object to proper JSON format
# The documents object should be directly JSON serializable from Meilisearch client
formatted_json = json.dumps(documents, indent=2, default=json_serializer)
return [
types.TextContent(type="text", text=f"Documents: {documents}")
types.TextContent(type="text", text=f"Documents:\n{formatted_json}")
]

elif name == "add-documents":
Expand Down
Loading
Loading