Is there some libraries similar to fastapi but for stdio comm
- User
Is there some libraries similar to fastapi but for stdio comm
Assistant
Okay, let's explore libraries and approaches that bring some of the benefits of FastAPI (like type hinting, validation, and a declarative style) to standard input/output (stdio) communication.
While there isn't a direct equivalent to FastAPI designed specifically and solely for stdio in the same way FastAPI targets HTTP, here are some libraries and concepts that offer similar philosophies or achieve related goals:
-
Typer:
- Analogy: This is arguably the closest in spirit and authorship to FastAPI. Typer is created by the same developer (Sebastián Ramírez) and heavily leverages Python type hints for defining command-line interfaces (CLIs).
- How it relates: CLIs inherently use stdio (arguments via
sys.argv, output viasys.stdout/stderr, sometimes input viasys.stdin). Typer uses type hints to automatically parse arguments, validate them (often integrating Pydantic models, just like FastAPI), provide default values, and generate help text. - Similarities to FastAPI: Declarative style using functions and type hints, automatic data validation/parsing, dependency injection concepts.
- Difference: Typer is focused on building interactive command-line applications, not necessarily long-running processes that continuously exchange structured data over stdio like a server might (though you could build such a thing using Typer as the entry point).
-
JSON-RPC Libraries over Stdio:
- Concept: A common pattern for structured communication between processes, especially tools and editors (like the Language Server Protocol), is to use JSON-RPC over stdio. You send JSON objects specifying a method and parameters via stdout, and receive responses/requests via stdin.
- Libraries:
python-jsonrpc-server: Can be adapted to listen/respond over stdio channels.pygls: Specifically implements the Language Server Protocol, which uses JSON-RPC over stdio. While LSP-specific, studying its structure shows how to handle complex, typed, asynchronous communication over stdio. You could potentially use parts of it or similar techniques.- Custom Implementation with Pydantic: You could build a loop that reads lines (or length-prefixed messages) from
sys.stdin, parses them as JSON, validates them using Pydantic models (similar to FastAPI request bodies), calls corresponding functions, serializes the response using Pydantic, converts to JSON, and writes tosys.stdout.
- Similarities to FastAPI: Can leverage Pydantic for validation and serialization based on type hints, allows defining clear "methods" or "endpoints" via the JSON-RPC structure.
- Difference: Requires setting up the stdio read/write loop and message framing (e.g., newline-delimited JSON, length-prefixing). Doesn't have built-in automatic API documentation like FastAPI's Swagger/ReDoc.
-
Using Pydantic Directly with Custom Stdio Handling:
- Concept: Implement the core communication loop yourself using
sys.stdin,sys.stdout, and potentiallyasynciofor asynchronous operations. Use Pydantic models to define the structure of messages you expect to receive and send. - How it works:
- Read data from
sys.stdin(e.g.,sys.stdin.readline()for line-based JSON). - Parse the input (e.g.,
json.loads()). - Validate the parsed data using a Pydantic model:
MyInputModel.model_validate(data). This gives you FastAPI-like validation. - Process the validated data and generate a response.
- If the response should be structured, create an instance of a Pydantic
MyOutputModel. - Serialize the response:
output_model.model_dump_json(). - Write the result to
sys.stdout.write()andsys.stdout.flush().
- Read data from
- Similarities to FastAPI: Uses Pydantic for robust data validation and serialization based on type hints.
- Difference: You are responsible for writing all the surrounding infrastructure (the read/write loop, dispatching logic, error handling, message framing).
- Concept: Implement the core communication loop yourself using
In Summary:
- If you need a robust CLI application with type-hinted argument parsing and validation, Typer is the closest philosophical sibling to FastAPI.
- If you need persistent, structured, request/response style communication between processes over stdio, implementing JSON-RPC (potentially using a library or building a custom handler with Pydantic) is a common and effective pattern. This gets you the structured data validation and serialization benefits similar to FastAPI.
There isn't a single library that perfectly mirrors FastAPI for stdio, mainly because the underlying communication paradigms (request-response HTTP vs. stream-based/message-based stdio) are quite different, leading to different design choices.
-
- User
Focus on decorator style definitions but not in field of mcp
Assistant
Okay, focusing specifically on libraries that use a decorator style for defining commands or message handlers for stdio communication, similar to how FastAPI uses decorators (
@app.get,@app.post) for defining HTTP endpoints, but avoiding niche protocols:-
Typer:
- Decorator Style: Yes, heavily. Uses
@app.command()to define CLI commands. - Stdio Relevance: Command-line applications fundamentally interact via stdio (arguments, options, stdin, stdout, stderr). Typer manages the parsing of
sys.argv(input arguments) based on function signatures defined under the decorators and directs output tostdout/stderr. You can also explicitly read fromsys.stdinwithin your command functions if needed. - FastAPI Similarity: Created by the same author, uses type hints for automatic parsing, validation (often via Pydantic), and help text generation, mirroring FastAPI's philosophy for CLI development.
- Example:
import typer import sys app = typer.Typer() @app.command() def process(name: str, count: int = 1): """ Processes a name multiple times. Reads additional data from stdin. """ print(f"Processing {name} {count} times.") # Example of reading from stdin within a command print("Enter additional data:") for line in sys.stdin: print(f"STDIN Data: {line.strip()}") if line.strip() == "EOF": # Simple exit condition break print(f"Finished processing {name}.") if __name__ == "__main__": app() - How it fits: While primarily for building CLIs, its decorator-based definition and type-hint-driven parsing are very much in the spirit of FastAPI, applied to the stdio-centric world of command lines.
- Decorator Style: Yes, heavily. Uses
-
JSON-RPC Libraries (with Decorator Support):
- Concept: Implement a request/response cycle over stdio using JSON-RPC. A message comes in on
stdin(e.g.,{"jsonrpc": "2.0", "method": "add", "params": [1, 2], "id": 1}), your application calls the corresponding function, and sends a response viastdout(e.g.,{"jsonrpc": "2.0", "result": 3, "id": 1}). - Decorator Style: Some JSON-RPC server libraries allow registering methods using decorators.
- Libraries:
python-jsonrpc-server: This library can often be adapted for stdio. It typically uses a dispatcher object where you can add methods, sometimes via decorators depending on the exact wrapper/framework you use it with (e.g.,dispatcher.add_method(my_func, name="func_name")or potentially@dispatcher.addin some setups). You'd need to write the stdio read/write loop around it.jsonrpcserver: Another popular library. It usesmethods.addfor registration, which can sometimes be used as a decorator (@methods.add). Again, you would typically wrap this in a custom stdio read/write loop.
- FastAPI Similarity: Mimics the request/response cycle, uses structured data (JSON), and the decorator approach provides a declarative way to define available "methods" or "endpoints". Can be combined with Pydantic in the decorated functions for validation.
- Example (Conceptual with
jsonrpcserver):import sys import json from jsonrpcserver import methods, dispatch @methods.add # Decorator style registration def add(a: int, b: int) -> int: """Adds two numbers.""" return a + b # Basic stdio loop (needs error handling, proper framing) if __name__ == "__main__": while True: line = sys.stdin.readline() if not line: break try: request_str = line.strip() response = dispatch(request_str) # Dispatch based on decorated methods if response.wanted: print(json.dumps(response.deserialized())) sys.stdout.flush() except Exception as e: # Handle parsing errors, dispatch errors etc. print(f"Error processing request: {e}", file=sys.stderr) - How it fits: Directly addresses the structured message handling over stdio using a declarative, decorator-based style for defining the functions that handle specific methods.
- Concept: Implement a request/response cycle over stdio using JSON-RPC. A message comes in on
-
Custom Solution with Decorators and Pydantic:
- Concept: You can relatively easily build your own dispatcher using decorators if existing libraries don't perfectly fit.
- Decorator Style: You define your own decorator (
@stdio_handler("method_name")) that registers functions in a dictionary. - Stdio Relevance: You explicitly write the loop reading
sys.stdin, parsing the input (e.g., JSON), looking up the handler in your registry, calling it (potentially validating arguments with Pydantic), and writing the response tosys.stdout. - FastAPI Similarity: You get the exact decorator style you want, and you can integrate Pydantic for validation, capturing key aspects of FastAPI's developer experience.
- Example:
import sys import json from pydantic import BaseModel, ValidationError HANDLERS = {} # Registry for decorated functions def stdio_handler(method_name): """Decorator to register a handler function.""" def decorator(func): HANDLERS[method_name] = func return func return decorator class AddParams(BaseModel): a: int b: int @stdio_handler("add") def handle_add(params: dict) -> int: """Handles the 'add' method with Pydantic validation.""" validated_params = AddParams(**params) # Validation happens here return validated_params.a + validated_params.b # Basic stdio loop if __name__ == "__main__": while True: line = sys.stdin.readline() if not line: break try: request = json.loads(line.strip()) method = request.get("method") params = request.get("params") req_id = request.get("id") if method in HANDLERS: result = HANDLERS[method](params) response = {"jsonrpc": "2.0", "result": result, "id": req_id} else: response = {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": req_id} print(json.dumps(response)) sys.stdout.flush() except json.JSONDecodeError: print(json.dumps({"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": None})) sys.stdout.flush() except ValidationError as e: print(json.dumps({"jsonrpc": "2.0", "error": {"code": -32602, "message": f"Invalid params: {e}"}, "id": req_id})) sys.stdout.flush() except Exception as e: print(json.dumps({"jsonrpc": "2.0", "error": {"code": -32603, "message": f"Internal error: {e}"}, "id": req_id})) sys.stdout.flush() - How it fits: Offers maximum flexibility and directly implements the requested decorator pattern for stdio message handling, often combined with Pydantic for a very FastAPI-like feel regarding data validation.
Conclusion:
- Typer is the most polished and FastAPI-like option if your stdio interaction fits the model of a Command Line Interface.
- JSON-RPC libraries (like
jsonrpcserveror adaptedpython-jsonrpc-server) provide a structured protocol and often support decorator-based method registration suitable for ongoing process-to-process communication over stdio. - A custom decorator implementation combined with Pydantic gives you full control and directly mimics the desired pattern if existing libraries aren't quite right.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.