Advertisement
Guest User

mcp-tools.py

a guest
Jun 1st, 2025
323
0
353 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | Source Code | 0 0
  1. import os
  2. import sys, logging, platform, socket, getpass
  3. from datetime import datetime
  4. from typing import Any
  5. import httpx
  6. import subprocess
  7. from mcp.server.fastmcp import FastMCP
  8.  
  9. __author__ = """Mark Pesce [email protected]"""
  10. __date__ = '$Date: 2025/05/24 20:37:00 $'.split()[1].replace('/', '-')
  11. __version__ = '$Revision: 0.1 $'.split()[1]
  12.  
  13. # reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8
  14. if sys.platform == "win32" and os.environ.get('PYTHONIOENCODING') is None:
  15.     sys.stdin.reconfigure(encoding="utf-8")
  16.     sys.stdout.reconfigure(encoding="utf-8")
  17.     sys.stderr.reconfigure(encoding="utf-8")
  18.  
  19. # Set up the logging
  20. logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p', level=logging.DEBUG)
  21.  
  22. # Initialize FastMCP server
  23. mcp = FastMCP("mcp-tools")
  24.  
  25. @mcp.tool()
  26. async def shell(command: str) -> str:
  27.     """Pass a shell command for execution
  28.  
  29.     Args:
  30.         command: A well-formatted bash command line
  31.  
  32.     Returns:
  33.         Output from the shell of the command execution
  34.  
  35.     TODO:
  36.         Make it stream output back to the LLM
  37.     """
  38.     try:
  39.         result = subprocess.run(
  40.             command,
  41.             shell=True,
  42.             capture_output=True,
  43.             text=True,
  44.             check=True
  45.             )
  46.         return result.stdout
  47.     except subprocess.CalledProcessError as e:
  48.         # Optionally return stderr or raise the exception
  49.         return f"{e.stderr}"
  50.  
  51. if __name__ == "__main__":
  52.     # Initialize and run the server
  53.     mcp.run(transport='stdio')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement