Skip to main content

Add STDIO Tool to AI Agent

Quick Start

1

Create a client file

Create a new file calculator_client.py with the following code:
The command string is shell-split, so a multi-word string can be combined with args=[...] — MCP("npx -y @modelcontextprotocol/server-filesystem", args=["/tmp"]) appends the extra args after the tokens from the string. All three forms in Passing runtime arguments produce the same subprocess.
2

Set Up STDIO MCP Server

Create a file calculator_server.py with the following code:
3

Install Dependencies

Make sure you have the required packages installed:
4

Export API Key

5

Run the Agent

Run the agent which will automatically start the calculator server:
Requirements
  • Python 3.10 or higher
  • MCP package

Cleanup

For one-off scripts the calculator subprocess is reclaimed when the Python process exits, so no manual cleanup is required. For long-running hosts (gateway, API server, notebook, CI), close the agent so its MCP subprocess is terminated deterministically:
Prefer the context-manager form when the agent’s scope is a single request:
See MCP Lifecycle Management for details.

Auto-respawn on crash

If a stdio MCP server crashes mid-session, PraisonAI respawns it on the next call.
  • Closed pipes, broken connections, and EOF are detected as a lost server.
  • The call that saw the crash returns Error: MCP server connection lost: <reason> immediately, without waiting out timeout.
  • The next call starts a fresh server with the same parameters and timeout.
  • Respawns are capped at 3 per MCP(...) instance.
  • A respawn that fails to start can be retried on a later call within the same budget.
  • Resource and prompt helpers (list_mcp_resources, read_mcp_resource, list_mcp_prompts, get_mcp_prompt) follow the restart too.
  • After shutdown() begins, no replacement is started.
The call that saw the crash is not replayed. It may already have reached the server, and retrying could repeat side effects. Your agent sees the error string; the next call gets the fresh server.
Each respawn logs MCP stdio server lost (<reason>); restarting (<n>/3).

Alternative LLM Integrations

Using Groq with STDIO

Using Ollama with STDIO

Gradio UI Integration

Create a Gradio UI for your calculator service:

Features

Simple Integration

Use standard input/output for easy integration with any tool or service.

Cross-Platform

Works on any operating system that supports Python.

Multiple LLM Options

Use with OpenAI, Groq, Ollama, or other supported LLMs.

Gradio UI

Create user-friendly interfaces for your STDIO integrations.