Skip to content

Commit f881eac

Browse files
Merge pull request #58 from joaopauloschuler/bpsa
Update main from bpsa
2 parents 4e8cfc3 + 59b7dc9 commit f881eac

7 files changed

Lines changed: 70 additions & 18 deletions

File tree

CLI.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ All prefixed with `BPSA_`:
6060
| `BPSA_MAX_TOKENS` | No | `64000` | Max tokens for model responses |
6161
| `BPSA_VERBOSE` | No | `0` | Verbose output (`0` or `1`) |
6262
| `BPSA_INJECT_FOLDER` | No | `true` | Inject directory tree (`false`, `true` = cwd, or a path) |
63+
| `BPSA_MCP` | No | `''` | Newline-separated list of MCP servers (URLs or stdio commands). Merged with `--mcp` CLI flags. |
6364

6465
### Context Compression Variables
6566

@@ -186,6 +187,34 @@ Use `prompt_toolkit` for:
186187
| `/verbose` | Toggle verbose output |
187188
| `/dictation [on\|off]` | Toggle dictation (requires `BPSA_DICTATION_TRANSCRIBER`) |
188189

190+
## MCP Server Integration
191+
192+
The `--mcp` flag connects [Model Context Protocol](https://modelcontextprotocol.io) servers as additional tool sources. Tools exposed by MCP servers are automatically available to the agent alongside the built-in tools.
193+
194+
```bash
195+
# HTTP-based MCP server (Streamable HTTP transport)
196+
bpsa --mcp http://localhost:8000/mcp
197+
198+
# stdio-based MCP server (shell command)
199+
bpsa --mcp 'npx -y @modelcontextprotocol/server-filesystem /'
200+
201+
# Multiple MCP servers (flag can be repeated)
202+
bpsa --mcp http://server1/mcp --mcp http://server2/mcp
203+
```
204+
205+
The flag can be repeated to connect multiple servers simultaneously. Each server's tools are merged into the agent's tool list. MCP connections are automatically closed when the session ends.
206+
207+
You can also set MCP servers persistently via the `BPSA_MCP` environment variable (one entry per line):
208+
209+
```bash
210+
export BPSA_MCP="http://localhost:8000/mcp"
211+
# or multiple servers:
212+
export BPSA_MCP="http://server1/mcp
213+
npx -y @modelcontextprotocol/server-filesystem /"
214+
```
215+
216+
CLI `--mcp` entries and `BPSA_MCP` entries are merged, so both can be used simultaneously.
217+
189218
## Configuration Layering
190219

191220
Priority (highest to lowest):

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
* 🗜️ **Context compression**: Biologically inspired [automatic LLM-based summarization](docs/compression.md) of older memory steps to manage context window size during long-running tasks.
2222
* 🌐 **Browser integration:** Control a headed Chromium browser from agent code blocks via Playwright (`--browser` flag).
2323
* 🖥️ **GUI interaction:** Launch, screenshot, click, type, and send keys to native GUI applications on X11 via xdotool/ImageMagick (`--gui-x11` flag).
24+
* 🔌 **MCP server integration:** Connect any [Model Context Protocol](https://modelcontextprotocol.io) server as a tool source via the `--mcp` CLI flag. Supports both HTTP (Streamable HTTP) and stdio-based servers.
2425
* 👁️ **Image loading:** Agents can load and visually inspect image files (plots, screenshots, diagrams) via the built-in `load_image` tool — always available, no flags needed.
2526
* 🎨 **Image tools:** Visual image diffing (`diff_images`), OCR text extraction from images (`screen_ocr`), and a canvas for drawing shapes, text, and annotations (`canvas_create`, `canvas_draw`) — always available.
2627
* 🎤 **Dictation input:** Dictate prompts via microphone using Whisper or ElevenLabs transcription (`/dictation` command, requires `BPSA_DICTATION_TRANSCRIBER` env var).
@@ -89,6 +90,8 @@ $ echo "task" | bpsa # Piped input
8990
$ bpsa --load-instructions # Load CLAUDE.md, AGENTS.md, etc. at startup
9091
$ bpsa --browser # Enable Playwright browser integration
9192
$ bpsa --gui-x11 # Enable native GUI interaction (xdotool/ImageMagick)
93+
$ bpsa --mcp http://localhost:8000/mcp # Connect an HTTP MCP server
94+
$ bpsa --mcp 'npx -y @modelcontextprotocol/server-filesystem /' # Connect a stdio MCP server
9295
```
9396

9497
The REPL supports command history, tab completion for slash commands, and multi-line input via Alt+Enter. Use `/session-save <file>` and `/session-load <file>` to persist and restore sessions across restarts. You can also launch `ad-infinitum` from within the REPL via `!ad-infinitum ...`. Type `/help` to see all available commands.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "bpsa"
7-
version = "1.23.11"
7+
version = "1.23.12"
88
description = "Beyond Python SmolAgents (BPSA) — a multi-language, multi-agent framework forked from HuggingFace smolagents."
99
authors = [
1010
{ name="Joao Paulo Schwarz Schuler" },

src/smolagents/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17-
__version__ = "1.23.11"
17+
__version__ = "1.23.12"
1818

1919
from .agent_types import * # noqa: I001
2020
from .agents import * # Above noqa avoids a circular dependency due to cli.py

src/smolagents/agents.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -870,20 +870,29 @@ def write_memory_to_messages(
870870
that can be used as input to the LLM. Adds a number of keywords (such as PLAN, error, etc) to help
871871
the LLM. If the agent has accumulated knowledge, it is injected just before the last message.
872872
"""
873-
messages = self.memory.system_prompt.to_messages(summary_mode=summary_mode)
873+
# Collect all memory step messages
874+
step_messages = []
874875
for memory_step in self.memory.steps:
875-
messages.extend(memory_step.to_messages(summary_mode=summary_mode))
876-
877-
# Inject knowledge near the end of context (just before the last message)
878-
if self.memory.knowledge and self.memory.knowledge.strip():
879-
knowledge_msg = ChatMessage(
880-
role=MessageRole.USER,
881-
content=[{"type": "text", "text": f"<knowledge>\n{self.memory.knowledge}\n</knowledge>"}],
882-
)
883-
if len(messages) > 1:
884-
messages.insert(len(messages) - 1, knowledge_msg)
885-
else:
886-
messages.append(knowledge_msg)
876+
step_messages.extend(memory_step.to_messages(summary_mode=summary_mode))
877+
878+
# New order: memory steps, system prompt, knowledge, last message
879+
if step_messages:
880+
last_message = step_messages[-1]
881+
messages = step_messages[:-1]
882+
messages.extend(self.memory.system_prompt.to_messages(summary_mode=summary_mode))
883+
if self.memory.knowledge and self.memory.knowledge.strip():
884+
messages.append(ChatMessage(
885+
role=MessageRole.SYSTEM,
886+
content=[{"type": "text", "text": f"<knowledge>\n{self.memory.knowledge}\n</knowledge>"}],
887+
))
888+
messages.append(last_message)
889+
else:
890+
messages = self.memory.system_prompt.to_messages(summary_mode=summary_mode)
891+
if self.memory.knowledge and self.memory.knowledge.strip():
892+
messages.append(ChatMessage(
893+
role=MessageRole.SYSTEM,
894+
content=[{"type": "text", "text": f"<knowledge>\n{self.memory.knowledge}\n</knowledge>"}],
895+
))
887896

888897
return messages
889898

src/smolagents/bp_cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2407,7 +2407,9 @@ def main():
24072407
from smolagents.bp_utils import get_env_bool
24082408
browser_enabled = args.browser or get_env_bool("BPSA_BROWSER")
24092409
gui_enabled = args.gui_x11 or get_env_bool("BPSA_GUI")
2410-
mcp_servers = _parse_mcp_servers(args.mcp or []) or None
2410+
env_mcp = get_env("BPSA_MCP", "")
2411+
env_mcp_list = [s.strip() for s in env_mcp.splitlines() if s.strip()]
2412+
mcp_servers = _parse_mcp_servers((args.mcp or []) + env_mcp_list) or None
24112413

24122414
# Piped input detection
24132415
if not sys.stdin.isatty() and args.command is None:

src/smolagents/bp_compression.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ def create_compression_prompt(
418418

419419
return f"""Hello super-intelligence!
420420
This task is involved in your context compression.
421-
To your own benefit, please summarize the following agent execution history into a concise summary.
421+
Please summarize the following agent execution history into a concise summary.
422+
Note: after compression, the original steps will be permanently removed from context. Write as if the reader will never see the originals.
422423
{COMMON_COMPRESSION_INSTRUCTIONS}
423424
{history_section}{knowledge_section}{post_steps_section}{output_instruction}
424425
This is the execution history to summarize:
@@ -461,7 +462,14 @@ def create_merge_prompt(compressed_steps: list[CompressedHistoryStep]) -> str:
461462
{summaries_text}
462463
</SUMMARIES_TO_MERGE>
463464
464-
CONSOLIDATED SUMMARY:"""
465+
Output format:
466+
<summary>
467+
Your consolidated summary of all events and changes...
468+
</summary>
469+
<knowledge_updates>
470+
...tagged updates if any...
471+
</knowledge_updates>
472+
"""
465473

466474

467475

@@ -621,6 +629,7 @@ def create_knowledge_extraction_prompt(
621629

622630
return f"""Hello super-intelligence!
623631
This task is involved in your context compression.
632+
Note: after compression, the original summaries will be permanently removed from context. Write as if the reader will never see the originals.
624633
Please extract key knowledge from the following {len(compressed_steps)} summaries
625634
covering {total_steps} total steps of agent execution.
626635
These summaries are about to be removed from the context. Therefore, updating the knowledge

0 commit comments

Comments
 (0)