|
| 1 | +# Model context protocol server |
| 2 | + |
| 3 | +A Mcp server is available to access events. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +To use the Mcp server in your app install [Django Mcpx](https://github.com/synw/django-mcpx) and |
| 8 | +add it to INSTALLED_APPS in settings: |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install django-mcpx |
| 12 | +``` |
| 13 | + |
| 14 | +```python settings.py |
| 15 | +INSTALLED_APPS = [ |
| 16 | + "mqueue", |
| 17 | + 'mcpx', |
| 18 | +] |
| 19 | +``` |
| 20 | + |
| 21 | +Declare the Mcp server and an auth token in settings: |
| 22 | + |
| 23 | +```python settings.py |
| 24 | +MCP_SERVERS = ["mqueue.mcpserver.mcp_server"] |
| 25 | + |
| 26 | +MCP_AUTH = "fefe865fe4856ferqsijjfhe-fre5qxpokjnEEZ5" # your auth token |
| 27 | +MCP_PORT = 8397 # optional |
| 28 | +MCP_HOST = "localhost" # optional |
| 29 | +``` |
| 30 | + |
| 31 | +## Run |
| 32 | + |
| 33 | +```bash |
| 34 | +python manage.py runmcp --host localhost --port 8397 |
| 35 | +``` |
| 36 | + |
| 37 | +This will start the MCP servers on `localhost` at port `8397`. |
| 38 | + |
| 39 | +Test it with a client or Mcp inspector: |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "My Mcp server": { |
| 44 | + "command": "npx", |
| 45 | + "args": [ |
| 46 | + "mcp-remote", |
| 47 | + "http://localhost:8397/mcp", |
| 48 | + "--header", |
| 49 | + "Authorization: Bearer fefe865fe4856ferqsijjfhe-fre5qxpokjnEEZ5" |
| 50 | + ] |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Tools |
| 56 | + |
| 57 | +The Mcp server provides the following tools to interact with events: |
| 58 | + |
| 59 | +### query_events_since |
| 60 | + |
| 61 | +**Description:** Query for events in the database since a given date. |
| 62 | + |
| 63 | +**Parameters:** |
| 64 | +- `start_date` (str): The start date for the query (use a standard Python date string). |
| 65 | + |
| 66 | +**Example Usage:** |
| 67 | +```json |
| 68 | +{ |
| 69 | + "command": "query_events_since", |
| 70 | + "args": { |
| 71 | + "start_date": "2023-10-01" |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### query_events |
| 77 | + |
| 78 | +**Description:** Query for events in the database. |
| 79 | + |
| 80 | +**Parameters:** |
| 81 | +- `nevents` (int): Maximum number of events to retrieve. |
| 82 | + |
| 83 | +**Example Usage:** |
| 84 | +```json |
| 85 | +{ |
| 86 | + "command": "query_events", |
| 87 | + "args": { |
| 88 | + "nevents": 10 |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Customizing Queries |
| 94 | + |
| 95 | +You can customize the queries by creating your own custom MCP server file. Below is an example of how you can achieve this by limiting the queries to events in a specific bucket. |
| 96 | + |
| 97 | +### Example: Limiting Queries to a Specific Bucket |
| 98 | + |
| 99 | +Create a new Python file, for example, `my_app.mcpserver.py`, and add the following code: |
| 100 | + |
| 101 | +```py custom_mcpserver.py |
| 102 | +from typing import Any, Dict |
| 103 | +from fastmcp import FastMCP |
| 104 | +from pydantic import Field |
| 105 | +from django.core.exceptions import PermissionDenied |
| 106 | +from mcpx.auth import mcp_auth |
| 107 | +from mqueue.models import MEvent |
| 108 | +from mqueue.utils import formatEvent, validate_and_parse_date |
| 109 | + |
| 110 | +name = "Custom Mcp events Server" |
| 111 | +mcp = FastMCP(name=name) |
| 112 | + |
| 113 | +@mcp.tool() |
| 114 | +async def query_events_in_bucket( |
| 115 | + start_date: str = Field(description="The start date for the query (use a standard Python date string)"), |
| 116 | + bucket: str = Field(description="The value of the bucket to filter events by") |
| 117 | +) -> Dict[str, Any]: |
| 118 | + """Query for events in the database since a given date within a specific bucket""" |
| 119 | + try: |
| 120 | + mcp_auth() |
| 121 | + except PermissionDenied as e: |
| 122 | + print(f"Mcp authentication error: {e}") |
| 123 | + return {"error": "undefined"} |
| 124 | + parsed_date = validate_and_parse_date(start_date) |
| 125 | + results = [] |
| 126 | + async for row in MEvent.objects.select_related("user").filter( |
| 127 | + date_posted__date__gte=parsed_date, |
| 128 | + bucket=bucket |
| 129 | + ): |
| 130 | + results.append(formatEvent(row)) |
| 131 | + return {"result": results} |
| 132 | + |
| 133 | +mcp_server = { |
| 134 | + "name": name, |
| 135 | + "mcp": mcp, |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +Add your new custom MCP server to the `MCP_SERVERS` list in your Django settings file. |
| 140 | + |
| 141 | +```python settings.py |
| 142 | +MCP_SERVERS = [ |
| 143 | + "my_app.mcpserver.mcp_server", |
| 144 | +] |
| 145 | +``` |
| 146 | + |
0 commit comments