Expose AI Answers search and ask as Tool API tools for MCP (ai_answers_tool)
## Problem/Motivation
Part of #3615758. Related: #3615755, #3615759.
Dries asked for AI Answers to be callable from Claude as an MCP tool, for an image search and content search demo. The content, the permissions and the retrieval already live in Drupal, so an agent should be able to ask Drupal directly.
We don't need a custom MCP endpoint. [Tool API](https://www.drupal.org/project/tool) tools become MCP tools through [MCP Server](https://www.drupal.org/project/mcp_server) and [MCP Server Tool Bridge](https://www.drupal.org/project/mcp_server_tool_bridge):
- A `mcp_tool_config` entity maps a tool to the wire name `tool_api__<id>`.
- `#[ToolMethod]`, released in Tool 1.0.0-beta12, turns a service method into a tool, with access declared on the attribute.
Retrieval without the LLM already exists in AI Answers: every new conversation forces it through `forceRagSearch()`, `resolveRetrievedSources()` and `renderReferences()`. Those methods are `protected` on a `final` class, though, so a submodule can't call them.
## Proposed resolution
**Core (ai_answers)**
Add a public method:
```php
public function search(string $agentId, string $query, ?int $limit = NULL, bool $render = FALSE): array
```
It applies the same gates as `answer()`:
- the agent is enabled for answers
- the index comes from `RagSettingsResolver::resolve()` and is passed explicitly
- score threshold, optional rerank, entity cap
- entity `view` access for the current user
It returns references with a snippet (a truncated `Source::$chunk`) and the langcode. It doesn't render view modes unless asked, and it doesn't touch `AgentRunContext`, Langfuse or the conversation store. That keeps it clear of #3615755.
**Submodule `modules/ai_answers_tool`** (requires `drupal/tool:^1.0.0-beta12`)
`AnswerTools` service with 2 tools:
| Tool | Inputs | Output | Notes |
|---|---|---|---|
| `ai_answers.search` | `query`, `agent?`, `limit?` | `results[]`: `title, url, entity_type, entity_id, snippet, score, langcode` | Retrieval only, no LLM. The default for MCP clients, so 2 LLMs don't run. |
| `ai_answers.ask` | `question`, `agent?`, `conversation_id?` | `answer`, numbered `sources[]`, `no_answer`, `conversation_id`, `turn` | Calls `AnswerService::answer(..., channel: 'mcp')`, the JSON path, so the no-answer substitution applies. |
- Both tools use `ToolOperation::Read`, `permission: 'use ai answers'` and `access: 'agentAccess'` (the agent must be enabled for answers).
- `agent` is optional. With exactly 1 agent enabled for answers, that agent is used. With several, a call without `agent` fails with an error that lists the valid agent ids, and the tool description names them too, so the MCP client can retry with the right one. This keeps the single-agent demo simple and stays explicit on multi-agent sites.
Ship optional bridge config in `config/optional/`, so it installs only when the bridge is enabled:
- `mcp_server_tool_bridge.mcp_tool_config.ai_answers_search.yml` → `tool_api__ai_answers_search`
- `mcp_server_tool_bridge.mcp_tool_config.ai_answers_ask.yml` → `tool_api__ai_answers_ask`
The bridge needs PHP 8.3 and mcp_server 2.0.0-beta5 or later. The tools themselves only need `tool`, so ai_answers stays on PHP 8.2.
For a local demo, the STDIO transport is the simplest: `ddev drush mcp:server <demo-user>`. HTTP `/mcp` needs `access mcp server` plus OAuth.
## Remaining tasks
- [ ] Decide: ship the bridge config enabled or disabled
- [ ] Decide: log `search` calls in `ai_answers_log` too, or only `ask` turns
- [ ] Core `AnswerService::search()` with Kernel tests
- [ ] `ai_answers_tool` service and optional config
- [ ] Manual STDIO test from Claude Code
- [ ] Before starting: the dev site needs `drupal/tool` 1.0.0-beta12 or later (`ddev composer update drupal/tool -W`). Beta11 and older have no `#[ToolMethod]`, and `mcp_server_tool_bridge` requires beta12.
## Acceptance criteria
- `ai_answers.search` makes no chat call (asserted with the scripted provider).
- It only returns entities the calling user can view (an unpublished node is never returned).
- It only searches the agent's configured index.
- `ai_answers.ask` returns `no_answer: true` with the configured message when nothing is cited.
- With ai_answers_log (#3615759) enabled, `ask` turns are logged with `channel = mcp`.
- Both tools appear as `tool_api__ai_answers_search` and `tool_api__ai_answers_ask` in `tools/list` over `drush mcp:server`.
## User interface changes
None.
## API changes
- New public `AnswerService::search()`.
- New Tool API tools `ai_answers.search` and `ai_answers.ask`.
## Data model changes
2 optional `mcp_tool_config` config entities.
_[AI disclosure](https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquette/policy-on-the-use-of-ai-when-contributing-to-drupal#s-disclosure): Used AI to help with research and writing (finding the retrieval seam in AnswerService, reading the Tool API and MCP bridge code, drafting this issue). The reasoning and positions are my own, and I reviewed everything before posting._
issue
GitLab AI Context
Project: project/ai_answers
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/ai_answers/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/ai_answers
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD