feat(mcp): expose every keyword via Model Context Protocol#293
Open
chinmayajha wants to merge 3 commits into
Open
feat(mcp): expose every keyword via Model Context Protocol#293chinmayajha wants to merge 3 commits into
chinmayajha wants to merge 3 commits into
Conversation
Add an MCP server so AI clients (Claude Desktop, Claude Code, Cursor,
claude.ai web) can drive an Optics session end-to-end through the same
keyword surface CSV/YAML test cases use.
- optics_framework/mcp/ : Server("optics-framework") that fans out
across session / inspect / keyword capability handlers, mirroring the
mozark-mcp capability pattern.
- tools/keywords.py auto-generates one MCP tool per entry in
discover_keywords() and dispatches through expose_api.execute_keyword,
so MCP inherits the existing fallback / event / template logic
unchanged.
- Two transports: streamable HTTP (default; Starlette + raw ASGI route
at /mcp to avoid the slash-redirect that drops POST bodies) and stdio
for local clients.
- New `optics mcp [--transport http|stdio] [--host] [--port]
[--cors-origin]` CLI subcommand.
- Unit tests cover schema generation, blocklist, dispatch wiring, and
session handler plumbing.
- docs/usage/MCP_usage.md walks a new user from "what is MCP" through installing, picking a transport, wiring it into Claude Desktop / Claude Code / Cursor / claude.ai web, the full tool reference, an end-to-end example, security notes, and troubleshooting. - docs/architecture/mcp_layer.md documents the capability fan-out, the auto-generated keyword tools, both transports, the request flow through to ExecutionEngine, and how to add new tools / transports. - docs/usage/CLI_usage.md gets an 'optics mcp' section next to 'optics serve' with a pointer to the full guide. - mkdocs.yml wires both new pages into the Usage and Architecture nav.
Addresses six of seven items from PR review; defends the seventh.
1. CRITICAL — Optional[X] = None params no longer mis-flagged as
required. The fix is to discover keywords by re-introspecting
`optics_framework.api.*` ourselves rather than going through
`expose_api.discover_keywords()`, whose `KeywordParameter` model
collapses "no default" and "default=None" into the same `None`.
Working from `inspect.Signature` lets us use `Parameter.empty` as
the sentinel for required-ness. Verified: 28 real keywords had
default=None params previously mis-marked as required; now 0.
2. Handlers now raise on failure instead of returning `{"error": ...}`
JSON. The MCP SDK's `@server.call_tool()` decorator catches
exceptions and returns `CallToolResult(isError=True, content=...)`,
which is the documented way to signal tool failure to the client.
The previous return-error-JSON pattern would have been classified
isError=False, leaving the LLM to interpret the failure as success.
3. `SessionManager.list_session_ids()` added as a public accessor;
`tools/session.py` no longer reaches into `session_manager.sessions`
via getattr.
4. `server.call_tool` now dispatches via a route map built once at
import time (`{tool_name -> handler}`) instead of looping through
capability modules per call. Also catches duplicate-name collisions
at startup rather than silently routing to the first match.
5. `_json_type_for` (substring matching on stringified annotations)
replaced with `_annotation_to_schema` that uses
`typing.get_origin` / `typing.get_args` against real type objects.
Optional[X] unwraps to X; Optional[List[str]] becomes
`array of string`; Union[str, List[Any]] becomes `anyOf`; bool is
checked before int because issubclass(bool, int).
6. `_RawASGIRoute` kept — the reviewer's suggested alternative
(`Route + handler.handle_request(request.scope, request.receive,
request._send)`) uses Starlette's PRIVATE `_send` attribute, which
is strictly worse. Added a comment defending the choice.
7. `optics_start_session` schema now describes the nested
{source: {url, capabilities, enabled}} shape via oneOf instead of
`{"type": ["string", "object"]}`, giving the LLM something
concrete to generate against.
Tests: 10 new tests covering the bug fix, the raise contract, the
route map, the nested schema, and the new SessionManager method.
Total MCP tests: 20 (up from 10), all passing.
Docs: updated docs/architecture/mcp_layer.md with the new discovery
approach, the route-map dispatch, the error contract, and the testing
coverage list.
|
malto101
reviewed
Jun 9, 2026
| return ALL_TOOLS | ||
|
|
||
|
|
||
| @server.call_tool() |
Member
There was a problem hiding this comment.
i would suggest to list get pagesource, get screenshot, etc(anything related to fetching data on current screen) as a resource instead of tool
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Add an MCP server so AI clients (Claude Desktop, Claude Code, Cursor, claude.ai web) can drive an Optics session end-to-end through the same keyword surface CSV/YAML test cases use.
optics mcp [--transport http|stdio] [--host] [--port] [--cors-origin]CLI subcommand.