Skip to content

Commit b8d52bc

Browse files
authored
Merge pull request #7 from meilisearch/add-dynamic-connection
Add dynamic connection tooling
2 parents a204b1d + b81328a commit b8d52bc

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A Model Context Protocol (MCP) server for interacting with Meilisearch through L
88
- Settings configuration with templates for common use cases (e-commerce, content search, SaaS)
99
- Task monitoring and API key management
1010
- Built-in logging and monitoring tools
11+
- Dynamic connection configuration to switch between Meilisearch instances
1112

1213
## Installation
1314

@@ -33,8 +34,32 @@ uv pip install -e .
3334
### Environment Variables
3435

3536
```bash
36-
MEILI_HTTP_ADDR=http://localhost:7700 # Meilisearch URL
37-
MEILI_MASTER_KEY=your_master_key # Optional: Meilisearch API key
37+
MEILI_HTTP_ADDR=http://localhost:7700 # Default Meilisearch URL
38+
MEILI_MASTER_KEY=your_master_key # Optional: Default Meilisearch API key
39+
```
40+
41+
### Dynamic Connection Configuration
42+
43+
The server provides tools to view and update connection settings at runtime:
44+
45+
- `get-connection-settings`: View current connection URL and API key status
46+
- `update-connection-settings`: Update URL and/or API key to connect to a different Meilisearch instance
47+
48+
Example usage through MCP:
49+
```json
50+
// Get current settings
51+
{
52+
"name": "get-connection-settings"
53+
}
54+
55+
// Update connection settings
56+
{
57+
"name": "update-connection-settings",
58+
"arguments": {
59+
"url": "http://new-host:7700",
60+
"api_key": "new-api-key"
61+
}
62+
}
3863
```
3964

4065
### Running the Server

src/meilisearch_mcp/monitoring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async def get_system_information(self) -> Dict[str, Any]:
8181
"""Get system-level information"""
8282
try:
8383
version = self.client.get_version()
84-
stats = self.client.get_stats()
84+
stats = self.client.get_all_stats()
8585

8686
return {
8787
"version": version,

src/meilisearch_mcp/server.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,45 @@ def __init__(
3939
log_dir = os.path.expanduser("~/.meilisearch-mcp/logs")
4040

4141
self.logger = MCPLogger("meilisearch-mcp", log_dir)
42+
self.url = url
43+
self.api_key = api_key
4244
self.meili_client = MeilisearchClient(url, api_key)
4345
self.server = Server("meilisearch")
4446
self._setup_handlers()
4547

48+
async def update_connection(self, url: Optional[str] = None, api_key: Optional[str] = None):
49+
"""Update connection settings and reinitialize client if needed"""
50+
if url:
51+
self.url = url
52+
if api_key:
53+
self.api_key = api_key
54+
55+
self.meili_client = MeilisearchClient(self.url, self.api_key)
56+
self.logger.info("Updated Meilisearch connection settings", url=self.url)
57+
4658
def _setup_handlers(self):
4759
"""Setup MCP request handlers"""
4860

4961
@self.server.list_tools()
5062
async def handle_list_tools() -> list[types.Tool]:
5163
"""List available tools"""
5264
return [
65+
types.Tool(
66+
name="get-connection-settings",
67+
description="Get current Meilisearch connection settings",
68+
inputSchema={"type": "object", "properties": {}},
69+
),
70+
types.Tool(
71+
name="update-connection-settings",
72+
description="Update Meilisearch connection settings",
73+
inputSchema={
74+
"type": "object",
75+
"properties": {
76+
"url": {"type": "string", "optional": True},
77+
"api_key": {"type": "string", "optional": True},
78+
},
79+
},
80+
),
5381
types.Tool(
5482
name="health-check",
5583
description="Check Meilisearch server health",
@@ -241,7 +269,27 @@ async def handle_call_tool(
241269
) -> list[types.TextContent]:
242270
"""Handle tool execution"""
243271
try:
244-
if name == "create-index":
272+
if name == "get-connection-settings":
273+
return [
274+
types.TextContent(
275+
type="text",
276+
text=f"Current connection settings:\nURL: {self.url}\nAPI Key: {'*' * 8 if self.api_key else 'Not set'}",
277+
)
278+
]
279+
280+
elif name == "update-connection-settings":
281+
await self.update_connection(
282+
arguments.get("url"),
283+
arguments.get("api_key")
284+
)
285+
return [
286+
types.TextContent(
287+
type="text",
288+
text=f"Successfully updated connection settings to URL: {self.url}",
289+
)
290+
]
291+
292+
elif name == "create-index":
245293
result = await self.meili_client.indexes.create_index(
246294
arguments["uid"], arguments.get("primaryKey")
247295
)

0 commit comments

Comments
 (0)