Skip to main content

PTY Create Function for MCP Server

This implementation provides a cross-platform pty_create(sandbox_id: str) function for MCP (Model Context Protocol) servers, enabling terminal interaction with sandboxes.

Features

  • ✅ Cross-platform support: Works on macOS and Windows
  • ✅ Optional sandbox_id parameter: Reuse existing sandboxes or create new ones
  • ✅ MCP protocol integration: Full server/client implementation
  • ✅ Async/await support: Non-blocking terminal operations
  • ✅ Session management: Create, manage, and close multiple terminal sessions
  • ✅ Backward compatibility: Works even without agentsphere package

Installation

# Install basic dependencies
pip install -r requirements.txt

# Install agentsphere for full functionality (optional)
pip install agentsphere

Quick Start

Basic Usage

from pty_create import pty_create, get_terminal_session

# Create a new PTY session
session_id = pty_create()
print(f"Session ID: {session_id}")

# Get session object
session = get_terminal_session(session_id)

# Write to terminal
session.write(b"ls -la\n")

# Read from terminal
output = await session.read()
print(f"Output: {output}")

# Close session
await close_terminal_session(session_id)

With Specific Sandbox

# Create PTY session with specific sandbox
session_id = pty_create("sandbox_123")

Simple Terminal Mode (without agentsphere)

The function works even without the agentsphere package, providing a simple terminal interface:

# This will work even without agentsphere
session_id = pty_create()
session = get_terminal_session(session_id)

MCP Server Integration

Running the Server

# Start the MCP server
python examples/server_example.py server

Client Connection

from mcp_terminal import MCPClient

# Connect to server
client = MCPClient(host="localhost", port=8001)
await client.connect()

# Create session
session_id = await client.pty_create()

# Interact with terminal
await client.pty_write("ls -la\n")
output = await client.pty_read()

Interactive Client

# Run interactive client
python examples/server_example.py interactive

API Reference

pty_create(sandbox_id: Optional[str] = None) -> str

Create a new PTY terminal session.

Parameters:

  • sandbox_id (str, optional): Sandbox ID to connect to. If not provided, uses existing or creates new.

Returns:

  • str: Unique session identifier

Example:

session_id = pty_create()                    # Use default sandbox
session_id = pty_create("sandbox_123")        # Use specific sandbox

get_terminal_session(session_id: str) -> TerminalSession

Get a terminal session by ID.

Parameters:

  • session_id (str): The session ID returned by pty_create()

Returns:

  • TerminalSession: Terminal session object

Example:

session = get_terminal_session(session_id)
session.write(b"command\n")
output = await session.read()

list_terminal_sessions() -> dict

List all active terminal sessions.

Returns:

  • dict: Dictionary containing session information

Example:

sessions = list_terminal_sessions()
for session_id, info in sessions.items():
    print(f"Session {session_id}: PID {info['pid']}")

close_terminal_session(session_id: str) -> bool

Close a terminal session.

Parameters:

  • session_id (str): The session ID to close

Returns:

  • bool: True if closed successfully

Example:

success = await close_terminal_session(session_id)

TerminalSession Methods

write(data: bytes) -> None

Write data to the terminal.

Parameters:

  • data (bytes): Data to write

Example:

session.write(b"echo hello\n")

read() -> str

Read data from the terminal.

Returns:

  • str: Terminal output

Example:

output = await session.read()

resize(rows: int, cols: int) -> None

Resize the terminal.

Parameters:

  • rows (int): Number of rows
  • cols (int): Number of columns

Example:

await session.resize(40, 120)

close() -> None

Close the terminal session.

Example:

await session.close()

Examples

Example 1: Basic Usage

import asyncio
from pty_create import pty_create, get_terminal_session

async def main():
    # Create session
    session_id = pty_create()
    session = get_terminal_session(session_id)

    # Run commands
    session.write(b"ls -la\n")
    await asyncio.sleep(1)
    output = await session.read()
    print(f"Directory listing: {output}")

    # Close session
    await close_terminal_session(session_id)

asyncio.run(main())

Example 2: Multiple Sessions

async def multiple_sessions():
    # Create multiple sessions
    session1 = pty_create()
    session2 = pty_create("sandbox_456")

    # Get session objects
    s1 = get_terminal_session(session1)
    s2 = get_terminal_session(session2)

    # Run different commands
    s1.write(b"echo 'Session 1'\n")
    s2.write(b"echo 'Session 2'\n")

    # Wait for completion
    await asyncio.sleep(2)

    # Read outputs
    print(f"Session 1 output: {await s1.read()}")
    print(f"Session 2 output: {await s2.read()}")

    # Close all sessions
    await close_terminal_session(session1)
    await close_terminal_session(session2)

Example 3: MCP Server Integration

from mcp_terminal import TerminalMCPServer

async def run_server():
    server = TerminalMCPServer()
    await server.start_server(host="localhost", port=8001)
    print("MCP server running on ws://localhost:8001")

    # Keep server running
    await asyncio.Event().wait()

asyncio.run(run_server())

Testing

Run the test suite:

# Test basic functionality
python pty_create.py test

# Test examples
python examples/basic_usage.py full

Platform Compatibility

macOS

  • ✅ Full support with agentsphere
  • ✅ Unix PTY mechanisms
  • ✅ Terminal.app compatible

Windows

  • ✅ Basic support without agentsphere
  • ✅ Terminal integration
  • ✅ Command prompt compatible

Linux

  • ✅ Full support with agentsphere
  • ✅ Native PTY support

Error Handling

The function includes comprehensive error handling:

try:
    session_id = pty_create()
except Exception as e:
    print(f"Failed to create session: {e}")

Common errors:

  • agentsphere package not available: Install agentsphere for full functionality
  • Session creation failed: Check network connectivity and sandbox status
  • Permission denied: Check file permissions and sandbox access

Development

Project Structure

agentsphere-mcp-server/
├── terminal_manager.py      # Core terminal management
├── mcp_terminal.py          # MCP server implementation
├── pty_create.py            # Main API function
├── examples/                # Usage examples
│   ├── basic_usage.py       # Basic usage example
│   └── server_example.py    # MCP server example
├── requirements.txt         # Dependencies
└── README.md               # This file

Adding New Features

  1. Add methods to TerminalManager class
  2. Update MCP server handlers in TerminalMCPServer
  3. Add tests in examples directory
  4. Update documentation

License

This implementation is provided for educational and development purposes.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

Metadata

Release files for agentsphere-mcp-server 1.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentsphere-mcp-server 1.9.1
File Size Uploaded Provenance
agentsphere_mcp_server-1.9.1.tar.gz 83.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for agentsphere-mcp-server 1.9.1
File Interpreter ABI Platform
agentsphere_mcp_server-1.9.1-py3-none-any.whl Python 3 none any Details

Total release size: 116.2 kB

Release files / agentsphere_mcp_server-1.9.1.tar.gz

Download URL agentsphere_mcp_server-1.9.1.tar.gz
Size 83.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3f9b683d1ac7da8e2e484bd97ed3790cf205452544cc68755e5a599fae9b758c
BLAKE2b-256 checksum
How to use checksums
cc2813e8e43e7a182631ab1def3997bc2753beeb616373c4a07d2b0b02d70e70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Release files / agentsphere_mcp_server-1.9.1-py3-none-any.whl

Download URL agentsphere_mcp_server-1.9.1-py3-none-any.whl
Size 33.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22c54ca8aa6a09e1ba94a73d853d047d13fd9757bb0a9120bb7fb458126508f8
BLAKE2b-256 checksum
How to use checksums
463a5cb7ab936baf6b74699c2b147e9990652546222f45c1d748ccb5e9d1205d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Security for agentsphere-mcp-server 1.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page