Repository navigation
Conversation
…d secfoo-memory The api agent was a fixed collect -> assess -> request_fix pipeline with no tool loop. MCP servers from [[mcp_servers]] (and secfoo-memory) only worked with --agent claude, so in CI, where api is the norm, --confluence pages could only be reported as "could not be reached". The api agent now connects to every server from effective_servers() with the official mcp SDK (stdio, streamable HTTP, SSE). One worker thread holds the async sessions. The server tools go to LiteLLM as function tools, and a new `tool` node runs the model's calls: assess -> tool -> assess. - Read-only by construction: only tools annotated readOnlyHint are offered, unless the server entry lists tools in the new `tools = [...]` allowlist. secfoo-memory lists its three lookups. - Bounded: 20 tool calls and 5 minutes per run. After that the model is told the budget is spent and the last call uses tool_choice=none. - Tool results count toward --max-input-tokens, which is now checked before every completion call. A result over 40,000 characters is truncated with a visible marker. A result that would exceed the limit is dropped and ends tool use. - Every call is logged (server, tool, arguments, bytes, status) to stderr.log. The same log goes back to the model, so the coverage section can only claim documents a tool returned. - Tokens and cost add up over every completion call. - api now counts as memory-capable, so memory guidance is rendered. Tests use a small FastMCP stdio server and a fake LiteLLM. Docs (README, REFERENCE, agents.md, config.example.toml) say api supports MCP. Fixes secfoo-com#123.
Only the errors that can actually end the MCP client's event loop are caught now: Exception, asyncio.CancelledError and BaseExceptionGroup (the builtin on 3.11+, the exceptiongroup backport on 3.10). KeyboardInterrupt and SystemExit are no longer swallowed. Tests cover both cases.
|
Thanks @github-advanced-security[bot]. Ran |
|
Thanks, this is a careful piece of work, and it closes the biggest gap in the "context, not just code" story. Things we especially liked:
CI is green on all platforms. One design point before we merge, then housekeeping:
Nits, fine to leave:
|
Closes #123
What does this change, and why?
The README says Confluence, Jira and SharePoint reach the agent through MCP, but that was only true for
--agent claude. Theapiagent is the GitHub Action default and the only option without a coding-agent CLI. It was a fixedcollect_files -> assess -> request_fixpipeline with no tool loop.--confluencepages could only be reported as "could not be reached", and the secfoo-memory tools were never available.This PR gives the
apiagent its own MCP client and a tool loop:src/secfoo/agents/mcp_tools.py): uses the officialmcpSDK to connect to every server fromeffective_servers()over stdio, streamable HTTP or SSE, list its tools and run calls. The SDK is async and the graph is sync, so one worker thread owns the event loop. A single task opens, serves and closes all sessions, which anyio's task groups require. A server that can't be reached is skipped and named instderr.log. The run continues without it.src/secfoo/agents/api.py): a newtoolnode (assess -> tool -> assess). Server tools are passed tolitellm.completion(tools=...)as function tools. The budget is 20 calls and 5 minutes of tool use. After that the model is told the budget is spent, and the last call usestool_choice="none". Calls over the budget in the same turn still get an answer ("Not run"), so everytool_call_idis matched.readOnlyHint: trueare offered. A new optional per-servertools = [...]key in[[mcp_servers]]is an explicit allowlist that replaces the annotation check for that server. The default secfoo-memory server lists its three lookup tools. A tool the model names that wasn't offered is refused and logged.--max-input-tokens, and the size check now runs before every completion call. A result over 40,000 characters is truncated with a visible marker. A result that would push the conversation over the limit is dropped, and tool use ends.stderr.log(server, tool, argument summary, bytes returned, status). The same log goes back to the model after each round, and the system prompt tells it to describe a source as read only if the log shows it was retrieved.apiis now inMEMORY_CAPABLE_AGENTS, so the pattern-memory guidance is rendered for it.mcp>=1.12,<2is added to the base dependencies. The 2.x SDK reworks the client transports this module uses.docs/REFERENCE.md, theapirow indocs/agents.md, andconfig.example.toml.Not done here: the manual run against the Atlassian Rovo server with a screenshot of the coverage section, which the issue lists under "Done when". That needs real Atlassian and model credentials. The behaviour is covered with a fake stdio MCP server instead.
Testing
Ran
pytestin a clean Linux container: the 4 remaining failure(s) also occur on the base branch without this change; no new failures are introduced.pytest testspassesruff check .passesNew tests in
tests/test_agents/test_api.pyuse a small FastMCP stdio server (tests/test_agents/fake_mcp_server.py) and a fake LiteLLM. They check that:--confluenceURL leads to a tool call, and the result and log reach the model;--max-input-tokens;tests/test_settings.pycovers the newtoolskey, andtests/test_memory.pyis updated forapinow being memory-capable.This PR was prepared with AI assistance (Claude) and reviewed by me before submitting.