|
1 | 1 | import json
|
2 |
| -from typing import Union, Mapping, List, TYPE_CHECKING |
| 2 | +from typing import Optional |
3 | 3 | from redis.exceptions import RedisError
|
| 4 | +from pydantic_core import core_schema |
4 | 5 |
|
5 | 6 | from src.common.connection import RedisConnectionManager
|
6 | 7 | from src.common.server import mcp
|
7 | 8 |
|
8 |
| -# Define JsonType for type checking to match redis-py definition |
9 |
| -# Use object as runtime type to avoid issubclass() issues with Any in Python 3.10 |
10 |
| -if TYPE_CHECKING: |
11 |
| - JsonType = Union[ |
12 |
| - str, int, float, bool, None, Mapping[str, "JsonType"], List["JsonType"] |
13 |
| - ] |
14 |
| -else: |
15 |
| - # Use object at runtime to avoid MCP framework issubclass() issues |
16 |
| - JsonType = object |
| 9 | + |
| 10 | +# Custom type that accepts any JSON value but generates a proper schema |
| 11 | +class JsonValue: |
| 12 | + """Accepts any JSON-serializable value.""" |
| 13 | + |
| 14 | + @classmethod |
| 15 | + def __get_pydantic_core_schema__(cls, _source_type, _handler): |
| 16 | + """Define how Pydantic should validate this type.""" |
| 17 | + # Accept any value |
| 18 | + return core_schema.any_schema() |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def __get_pydantic_json_schema__(cls, _core_schema, _handler): |
| 22 | + """Define the JSON schema for this type.""" |
| 23 | + # Return a schema that accepts string, number, boolean, object, array, or null |
| 24 | + return { |
| 25 | + "anyOf": [ |
| 26 | + {"type": "string"}, |
| 27 | + {"type": "number"}, |
| 28 | + {"type": "boolean"}, |
| 29 | + {"type": "object"}, |
| 30 | + {"type": "array", "items": {"type": "string"}}, |
| 31 | + {"type": "null"}, |
| 32 | + ] |
| 33 | + } |
17 | 34 |
|
18 | 35 |
|
19 | 36 | @mcp.tool()
|
20 | 37 | async def json_set(
|
21 |
| - name: str, path: str, value: JsonType, expire_seconds: int = None |
| 38 | + name: str, |
| 39 | + path: str, |
| 40 | + value: JsonValue, |
| 41 | + expire_seconds: Optional[int] = None, |
22 | 42 | ) -> str:
|
23 | 43 | """Set a JSON value in Redis at a given path with an optional expiration time.
|
24 | 44 |
|
|
0 commit comments