Skip to content

feat(mcp): expose every keyword via Model Context Protocol#293

Open
chinmayajha wants to merge 3 commits into
mozarkai:mainfrom
chinmayajha:feat/mcp-server
Open

feat(mcp): expose every keyword via Model Context Protocol#293
chinmayajha wants to merge 3 commits into
mozarkai:mainfrom
chinmayajha:feat/mcp-server

Conversation

@chinmayajha

Copy link
Copy Markdown
Contributor

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.

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.
@sonarqubecloud

sonarqubecloud Bot commented Jun 3, 2026

Copy link
Copy Markdown

return ALL_TOOLS


@server.call_tool()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would suggest to list get pagesource, get screenshot, etc(anything related to fetching data on current screen) as a resource instead of tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants