Repository navigation
feat(mcp): validate and wrap async functions as code tools #759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
083a547
feat(mcp): validate and wrap async functions as code tools
vishal-bala 9717e52
fix(mcp): refuse a keyword-only context and re-check read-only per call
vishal-bala 9977626
fix(mcp): check a code tool's input schema where it is declared, and …
vishal-bala 74978d4
fix(mcp): require the read scope for writing code tools, and match to…
vishal-bala a147942
test(mcp): bound the queued-timeout test so a lost timeout fails inst…
vishal-bala ecf774a
fix(mcp): refuse a code tool scope with surrounding whitespace
vishal-bala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| """Code tools: async Python functions registered as MCP tools. | ||
|
|
||
| A code tool is an async function whose first parameter is a | ||
| :class:`~redisvl.mcp.context.ToolContext` and whose remaining, annotated | ||
| parameters are the arguments the model supplies. Its docstring (or an explicit | ||
| description) is what the model reads when choosing a tool. | ||
|
|
||
| Everything that makes a built-in safe is applied around the function rather | ||
| than left to its author: the read or write scope and any per-tool scope are | ||
| checked before it runs, the context it receives can only reach Redis through a | ||
| guarded, allow-listed handle, the whole call is bounded by the binding's | ||
| request timeout, and any exception it raises is mapped to the stable MCP error | ||
| contract instead of leaking a traceback. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import inspect | ||
| import typing | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| from pydantic import TypeAdapter | ||
|
|
||
| from redisvl.mcp.auth import ensure_read_scope, ensure_tool_scope, ensure_write_scope | ||
| from redisvl.mcp.config import validate_tool_name | ||
| from redisvl.mcp.context import ToolContext | ||
| from redisvl.mcp.errors import MCPErrorCode, RedisVLMCPError, map_exception | ||
|
|
||
| _ACCEPTED_KINDS = ( | ||
| inspect.Parameter.POSITIONAL_OR_KEYWORD, | ||
| inspect.Parameter.KEYWORD_ONLY, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class CodeToolSpec: | ||
| """One validated code tool, ready to register against a binding.""" | ||
|
|
||
| fn: Callable[..., Any] | ||
| name: str | ||
| description: str | ||
| index: str | None | ||
| writes: bool | ||
| scope: str | None | ||
| signature: inspect.Signature | ||
| annotations: dict[str, Any] | ||
|
|
||
|
|
||
| def _qualified(fn: Callable[..., Any]) -> str: | ||
| return f"{getattr(fn, '__module__', '?')}.{getattr(fn, '__qualname__', fn)}" | ||
|
|
||
|
|
||
| def _tool_signature( | ||
| fn: Callable[..., Any], | ||
| ) -> tuple[inspect.Signature, dict[str, Any]]: | ||
| """Return the model-facing signature: the function's, minus the context.""" | ||
| where = _qualified(fn) | ||
| try: | ||
| # Resolved rather than read raw, so a module using postponed | ||
| # annotations is checked against the real types. | ||
| hints = typing.get_type_hints(fn, include_extras=True) | ||
| except Exception as exc: | ||
| raise TypeError( | ||
| f"code tool {where} has annotations that cannot be resolved: {exc}" | ||
| ) from exc | ||
|
|
||
| parameters = list(inspect.signature(fn).parameters.values()) | ||
| # Positional, because the wrapper passes the context positionally; a | ||
| # keyword-only one would be accepted here and fail on every call. | ||
| if ( | ||
| not parameters | ||
| or hints.get(parameters[0].name) is not ToolContext | ||
| or parameters[0].kind | ||
| not in ( | ||
| inspect.Parameter.POSITIONAL_ONLY, | ||
| inspect.Parameter.POSITIONAL_OR_KEYWORD, | ||
| ) | ||
| ): | ||
| raise TypeError( | ||
| f"code tool {where} must take a ToolContext as its first, positional " | ||
| "parameter" | ||
| ) | ||
|
|
||
| exposed = [] | ||
| annotations: dict[str, Any] = {} | ||
| for parameter in parameters[1:]: | ||
| if parameter.kind not in _ACCEPTED_KINDS: | ||
| raise TypeError( | ||
| f"code tool {where} parameter '{parameter.name}' must be a plain " | ||
| "named parameter; *args, **kwargs and positional-only " | ||
| "parameters cannot be offered to a model" | ||
| ) | ||
| if parameter.name not in hints: | ||
| raise TypeError( | ||
| f"code tool {where} parameter '{parameter.name}' needs a type " | ||
| "annotation; it is what the tool's input schema is built from" | ||
| ) | ||
| annotation = hints[parameter.name] | ||
| try: | ||
| # Built here so a type the input schema cannot express fails at | ||
| # declaration, not at startup after every binding has connected. | ||
| TypeAdapter(annotation).json_schema() | ||
| except Exception as exc: | ||
| raise TypeError( | ||
| f"code tool {where} parameter '{parameter.name}' has a type " | ||
| f"that no input schema can describe: {exc}" | ||
| ) from exc | ||
| # Keyword-only, because the wrapper is always called with keyword | ||
| # arguments decoded from the model's JSON. | ||
| exposed.append( | ||
| parameter.replace( | ||
| kind=inspect.Parameter.KEYWORD_ONLY, annotation=annotation | ||
| ) | ||
| ) | ||
| annotations[parameter.name] = annotation | ||
|
|
||
| return_annotation = hints.get("return", inspect.Signature.empty) | ||
| if return_annotation is not inspect.Signature.empty: | ||
| annotations["return"] = return_annotation | ||
| return ( | ||
| inspect.Signature(exposed, return_annotation=return_annotation), | ||
| annotations, | ||
| ) | ||
|
|
||
|
|
||
| def build_code_tool_spec( | ||
| fn: Callable[..., Any], | ||
| *, | ||
| name: str | None = None, | ||
| namespace: str | None = None, | ||
| index: str | None = None, | ||
| writes: bool = False, | ||
| scope: str | None = None, | ||
| description: str | None = None, | ||
| ) -> CodeToolSpec: | ||
| """Validate ``fn`` as a code tool and return its spec. | ||
|
|
||
| Everything checkable without a running server is checked here, so a | ||
| malformed tool fails where it is declared rather than at startup. | ||
| Whether ``index`` names a real binding, and whether a writing tool is | ||
| allowed on it, depends on the server's config and is checked at startup. | ||
| """ | ||
| where = _qualified(fn) | ||
| if not inspect.iscoroutinefunction(fn): | ||
| raise TypeError(f"code tool {where} must be an async function") | ||
|
|
||
| signature, annotations = _tool_signature(fn) | ||
|
|
||
| resolved_description = ( | ||
| description if description is not None else inspect.getdoc(fn) | ||
| ) | ||
| if not resolved_description or not resolved_description.strip(): | ||
| raise ValueError( | ||
| f"code tool {where} needs a description: give it a docstring or pass " | ||
| "description=; it is what the model chooses tools by" | ||
| ) | ||
|
|
||
| resolved_name = name if name is not None else fn.__name__.replace("_", "-") | ||
| if namespace is not None: | ||
| resolved_name = f"{namespace}-{resolved_name}" | ||
| validate_tool_name(resolved_name, kind=f"code tool {where}") | ||
|
|
||
| if scope is not None and not scope.strip(): | ||
| raise ValueError(f"code tool {where} scope must not be blank") | ||
| if scope is not None and scope != scope.strip(): | ||
| # Scopes are matched exactly, so a padded one would refuse every caller. | ||
| raise ValueError( | ||
| f"code tool {where} scope {scope!r} has surrounding whitespace" | ||
| ) | ||
|
|
||
| return CodeToolSpec( | ||
| fn=fn, | ||
| name=resolved_name, | ||
| description=resolved_description.strip(), | ||
| index=index, | ||
| writes=writes, | ||
| scope=scope, | ||
| signature=signature, | ||
| annotations=annotations, | ||
| ) | ||
|
|
||
|
|
||
| def register_code_tool(server: Any, spec: CodeToolSpec, binding_id: str) -> None: | ||
| """Register one code tool against ``binding_id``.""" | ||
|
|
||
| async def code_tool(**kwargs: Any) -> Any: | ||
| # Before anything else, so a caller without the scope never reaches the | ||
| # binding or the function. The tool's own scope is required on top of | ||
| # the server-wide one, so it can only make a tool stricter. A writing | ||
| # tool needs the read scope too, because its handle can also read. | ||
| ensure_read_scope(server) | ||
| if spec.writes: | ||
| ensure_write_scope(server) | ||
| ensure_tool_scope(server, spec.scope) | ||
|
vishal-bala marked this conversation as resolved.
Resolved
|
||
|
|
||
| try: | ||
| runtime = server.resolve_binding(binding_id) | ||
| if spec.writes and runtime.effective_read_only: | ||
| # Startup already refuses this pairing; checked again per call, | ||
| # as upsert_records does, so a write never depends on that | ||
| # check alone. | ||
| raise RedisVLMCPError( | ||
| f"index '{binding_id}' is read-only", | ||
| code=MCPErrorCode.FORBIDDEN, | ||
| retryable=False, | ||
| ) | ||
| ctx = ToolContext.for_request(server, runtime, writes=spec.writes) | ||
| timeout = runtime.binding.runtime.request_timeout_seconds | ||
| try: | ||
| # The handle bounds each Redis call; this bounds the tool's own | ||
| # code too, so a tool cannot hold a request open indefinitely. | ||
| return await asyncio.wait_for(spec.fn(ctx, **kwargs), timeout) | ||
| except asyncio.TimeoutError as exc: | ||
| raise RedisVLMCPError( | ||
| f"tool '{spec.name}' did not finish within {timeout:g}s", | ||
| code=MCPErrorCode.BACKEND_UNAVAILABLE, | ||
| retryable=True, | ||
| ) from exc | ||
| except RedisVLMCPError: | ||
| raise | ||
| except Exception as exc: | ||
| raise map_exception(exc) from exc | ||
|
|
||
| code_tool.__name__ = spec.name.replace("-", "_") | ||
| code_tool.__doc__ = spec.description | ||
| code_tool.__signature__ = spec.signature # type: ignore[attr-defined] | ||
| code_tool.__annotations__ = dict(spec.annotations) | ||
|
|
||
| server.tool(name=spec.name, description=spec.description)(code_tool) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.