-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathconfig.py
More file actions
408 lines (332 loc) · 14.7 KB
/
config.py
File metadata and controls
408 lines (332 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import os
import re
from copy import deepcopy
from pathlib import Path
from typing import Any, Dict, Literal, Optional
import yaml
from pydantic import BaseModel, ConfigDict, Field, model_validator
from redisvl.schema.fields import BaseField
from redisvl.schema.schema import IndexSchema
_ENV_PATTERN = re.compile(r"\$\{([^}:]+)(?::-([^}]*))?\}")
class MCPRuntimeConfig(BaseModel):
"""Runtime limits and validated field mappings for MCP requests."""
text_field_name: str = Field(..., min_length=1)
vector_field_name: str = Field(..., min_length=1)
default_embed_text_field: str = Field(..., min_length=1)
default_limit: int = 10
max_limit: int = 100
max_upsert_records: int = 64
skip_embedding_if_present: bool = True
startup_timeout_seconds: int = 30
request_timeout_seconds: int = 60
max_concurrency: int = 16
@model_validator(mode="after")
def _validate_limits(self) -> "MCPRuntimeConfig":
"""Validate runtime bounds during config load."""
if self.default_limit <= 0:
raise ValueError("runtime.default_limit must be greater than 0")
if self.max_limit < self.default_limit:
raise ValueError(
"runtime.max_limit must be greater than or equal to runtime.default_limit"
)
if self.max_upsert_records <= 0:
raise ValueError("runtime.max_upsert_records must be greater than 0")
if self.startup_timeout_seconds <= 0:
raise ValueError("runtime.startup_timeout_seconds must be greater than 0")
if self.request_timeout_seconds <= 0:
raise ValueError("runtime.request_timeout_seconds must be greater than 0")
if self.max_concurrency <= 0:
raise ValueError("runtime.max_concurrency must be greater than 0")
return self
class MCPVectorizerConfig(BaseModel):
"""Vectorizer constructor contract loaded from YAML."""
model_config = ConfigDict(populate_by_name=True, extra="allow")
class_name: str = Field(alias="class", min_length=1)
model: str = Field(..., min_length=1)
@property
def extra_kwargs(self) -> Dict[str, Any]:
"""Return vectorizer kwargs other than the normalized `class` and `model`."""
return dict(self.model_extra or {})
def to_init_kwargs(self) -> Dict[str, Any]:
"""Build kwargs suitable for directly instantiating the vectorizer."""
return {"model": self.model, **self.extra_kwargs}
class MCPServerConfig(BaseModel):
"""Server-level bootstrap configuration."""
redis_url: str = Field(..., min_length=1)
class MCPIndexSearchConfig(BaseModel):
"""Configured search mode and query tuning for the bound index.
The MCP request contract only exposes query text, filtering, pagination, and
field projection. Search mode and query-tuning behavior are owned entirely by
YAML config and validated here.
"""
type: Literal["vector", "fulltext", "hybrid"]
params: Dict[str, Any] = Field(default_factory=dict)
@model_validator(mode="after")
def _validate_params(self) -> "MCPIndexSearchConfig":
"""Reject params that do not belong to the configured search mode."""
allowed_params = {
"vector": {
"hybrid_policy",
"batch_size",
"ef_runtime",
"epsilon",
"search_window_size",
"use_search_history",
"search_buffer_capacity",
"normalize_vector_distance",
},
"fulltext": {
"text_scorer",
"stopwords",
"text_weights",
},
"hybrid": {
"text_scorer",
"stopwords",
"text_weights",
"vector_search_method",
"knn_ef_runtime",
"range_radius",
"range_epsilon",
"combination_method",
"rrf_window",
"rrf_constant",
"linear_text_weight",
},
}
invalid_keys = sorted(set(self.params) - allowed_params[self.type])
if invalid_keys:
raise ValueError(
"search.params contains keys incompatible with "
f"search.type '{self.type}': {', '.join(invalid_keys)}"
)
if (
"linear_text_weight" in self.params
and self.params.get("combination_method") != "LINEAR"
):
raise ValueError(
"search.params.linear_text_weight requires combination_method to be LINEAR"
)
return self
def to_query_params(self) -> Dict[str, Any]:
"""Return normalized query kwargs exactly as configured."""
return dict(self.params)
def validate_runtime_capabilities(
self, *, supports_native_hybrid_search: bool
) -> None:
"""Fail startup when hybrid config depends on native-only FT.SEARCH params."""
if self.type != "hybrid" or supports_native_hybrid_search:
return
unsupported_params = set()
if self.params.get("combination_method") not in (None, "LINEAR"):
unsupported_params.add("combination_method")
unsupported_params.update(
key
for key in (
"vector_search_method",
"knn_ef_runtime",
"range_radius",
"range_epsilon",
"rrf_window",
"rrf_constant",
)
if key in self.params
)
if unsupported_params:
unsupported_list = ", ".join(sorted(unsupported_params))
raise ValueError(
"search.params requires native hybrid search support for: "
f"{unsupported_list}"
)
class MCPSchemaOverrideField(BaseModel):
"""Allowed schema override fragment for one already-discovered field."""
name: str = Field(..., min_length=1)
type: str = Field(..., min_length=1)
path: Optional[str] = None
attrs: Dict[str, Any] = Field(default_factory=dict)
class MCPSchemaOverrides(BaseModel):
"""Optional field-level schema patches used to fill inspection gaps."""
fields: list[MCPSchemaOverrideField] = Field(default_factory=list)
class MCPIndexBindingConfig(BaseModel):
"""The sole configured v1 index binding."""
redis_name: str = Field(..., min_length=1)
vectorizer: MCPVectorizerConfig
search: MCPIndexSearchConfig
runtime: MCPRuntimeConfig
schema_overrides: MCPSchemaOverrides = Field(default_factory=MCPSchemaOverrides)
class MCPConfig(BaseModel):
"""Validated MCP server configuration loaded from YAML."""
server: MCPServerConfig
indexes: Dict[str, MCPIndexBindingConfig]
@model_validator(mode="after")
def _validate_bindings(self) -> "MCPConfig":
"""Validate that there is exactly one configured logical binding."""
if len(self.indexes) != 1:
raise ValueError(
"indexes must contain exactly one configured index binding"
)
binding_id = next(iter(self.indexes))
if not binding_id.strip():
raise ValueError("indexes binding id must be non-blank")
return self
@property
def binding_id(self) -> str:
"""Return the single logical binding identifier configured for v1."""
return next(iter(self.indexes))
@property
def binding(self) -> MCPIndexBindingConfig:
"""Return the sole configured binding."""
return self.indexes[self.binding_id]
@property
def runtime(self) -> MCPRuntimeConfig:
"""Expose the sole binding's runtime config for phase 1."""
return self.binding.runtime
@property
def vectorizer(self) -> MCPVectorizerConfig:
"""Expose the sole binding's vectorizer config for phase 1."""
return self.binding.vectorizer
@property
def search(self) -> MCPIndexSearchConfig:
"""Expose the sole binding's configured search behavior."""
return self.binding.search
@property
def redis_name(self) -> str:
"""Return the existing Redis index name that must be inspected at startup."""
return self.binding.redis_name
def inspected_schema_from_index_info(
self, index_info: Dict[str, Any]
) -> Dict[str, Any]:
"""Build a schema dict from FT.INFO while preserving discovered field identity.
RedisVL's generic FT.INFO conversion omits vector fields when their attrs are
incomplete on older Redis versions. MCP needs those field identities to survive
so schema overrides can patch the missing attrs during startup.
"""
from redisvl.redis.connection import convert_index_info_to_schema
schema_dict = convert_index_info_to_schema(index_info)
discovered_fields = {
field["name"]: field
for field in schema_dict.get("fields", [])
if isinstance(field, dict) and "name" in field
}
storage_type = index_info["index_definition"][1].lower()
for raw_field in index_info.get("attributes", []):
name = raw_field[1] if storage_type == "hash" else raw_field[3]
if name in discovered_fields:
continue
field = {
"name": name,
"type": str(raw_field[5]).lower(),
}
if storage_type == "json":
field["path"] = raw_field[1]
# Keep discovered field identity even when FT.INFO omitted attrs.
schema_dict.setdefault("fields", []).append(field)
return schema_dict
def merge_schema_overrides(
self, inspected_schema: Dict[str, Any]
) -> Dict[str, Any]:
"""Apply validated schema overrides without allowing identity changes."""
merged_schema = deepcopy(inspected_schema)
merged_fields = merged_schema.setdefault("fields", [])
discovered_fields = {
field["name"]: field
for field in merged_fields
if isinstance(field, dict) and "name" in field
}
for override in self.binding.schema_overrides.fields:
discovered = discovered_fields.get(override.name)
if discovered is None:
raise ValueError(
f"schema_overrides.fields '{override.name}' not found in inspected schema"
)
discovered_type = str(discovered.get("type", "")).lower()
override_type = override.type.lower()
if discovered_type != override_type:
raise ValueError(
f"schema_overrides.fields '{override.name}' cannot change discovered field type"
)
discovered_path = discovered.get("path")
if override.path is not None and override.path != discovered_path:
raise ValueError(
f"schema_overrides.fields '{override.name}' cannot change discovered field path"
)
if override.attrs:
merged_attrs = dict(discovered.get("attrs", {}))
merged_attrs.update(override.attrs)
discovered["attrs"] = merged_attrs
return merged_schema
def validate_runtime_mapping(self, schema: IndexSchema) -> None:
"""Ensure runtime mappings point at explicit fields in the effective schema."""
field_names = set(schema.field_names)
if self.runtime.text_field_name not in field_names:
raise ValueError(
f"runtime.text_field_name '{self.runtime.text_field_name}' not found in schema"
)
if self.runtime.default_embed_text_field not in field_names:
raise ValueError(
"runtime.default_embed_text_field "
f"'{self.runtime.default_embed_text_field}' not found in schema"
)
vector_field = schema.fields.get(self.runtime.vector_field_name)
if vector_field is None:
raise ValueError(
f"runtime.vector_field_name '{self.runtime.vector_field_name}' not found in schema"
)
if vector_field.type != "vector":
raise ValueError(
f"runtime.vector_field_name '{self.runtime.vector_field_name}' must reference a vector field"
)
def to_index_schema(self, inspected_schema: Dict[str, Any]) -> IndexSchema:
"""Apply overrides to an inspected schema and validate the effective result."""
merged_schema = self.merge_schema_overrides(inspected_schema)
schema = IndexSchema.model_validate(merged_schema)
self.validate_runtime_mapping(schema)
return schema
def get_vector_field(self, schema: IndexSchema) -> BaseField:
"""Return the effective vector field from a validated schema."""
return schema.fields[self.runtime.vector_field_name]
def get_vector_field_dims(self, schema: IndexSchema) -> Optional[int]:
"""Return the effective vector dimensions when the field exposes them."""
attrs = self.get_vector_field(schema).attrs
return getattr(attrs, "dims", None)
def validate_search(
self,
*,
supports_native_hybrid_search: bool,
) -> None:
"""Validate configured search behavior against current runtime support."""
self.search.validate_runtime_capabilities(
supports_native_hybrid_search=supports_native_hybrid_search
)
def _substitute_env(value: Any) -> Any:
"""Recursively resolve `${VAR}` and `${VAR:-default}` placeholders."""
if isinstance(value, dict):
return {key: _substitute_env(item) for key, item in value.items()}
if isinstance(value, list):
return [_substitute_env(item) for item in value]
if not isinstance(value, str):
return value
def replace(match: re.Match[str]) -> str:
name = match.group(1)
default = match.group(2)
env_value = os.environ.get(name)
if env_value is not None:
return env_value
if default is not None:
return default
raise ValueError(f"Missing required environment variable: {name}")
return _ENV_PATTERN.sub(replace, value)
def load_mcp_config(path: str) -> MCPConfig:
"""Load, substitute, and validate the MCP YAML configuration file."""
config_path = Path(path).expanduser()
if not config_path.exists():
raise FileNotFoundError(f"MCP config file {path} does not exist")
try:
with config_path.open("r", encoding="utf-8") as file:
raw_data = yaml.safe_load(file)
except yaml.YAMLError as exc:
raise ValueError(f"Invalid MCP config YAML: {exc}") from exc
if not isinstance(raw_data, dict):
raise ValueError("Invalid MCP config YAML: root document must be a mapping")
substituted = _substitute_env(raw_data)
return MCPConfig.model_validate(substituted)