"""MCP server for reading and writing clipboard data."""
import pyperclip
from mcp.server.fastmcp import FastMCP
__all__ = ["main", "mcp"]
__version__ = "0.1.0"
mcp = FastMCP(
"Clipboard",
instructions=(
"This server provides tools for reading and writing clipboard data. "
"Use `read_clipboard` to get the current clipboard contents and "
"`write_clipboard` to set new clipboard contents."
),
)
@mcp.tool()
def read_clipboard() -> str:
"""Read the current text content from the system clipboard.
Returns the text currently stored in the clipboard. If the clipboard
is empty or contains non-text data, an empty string is returned.
"""
try:
content = pyperclip.paste()
except pyperclip.PyperclipException as e:
msg = f"Failed to read clipboard: {e}"
raise RuntimeError(msg) from e
else:
return content if content else ""
@mcp.tool()
def write_clipboard(text: str) -> str:
"""Write text content to the system clipboard.
Copies the provided text to the system clipboard, making it available
for pasting in other applications.
Args:
text: The text content to write to the clipboard.
Returns:
A confirmation message indicating success.
"""
try:
pyperclip.copy(text)
except pyperclip.PyperclipException as e:
msg = f"Failed to write to clipboard: {e}"
raise RuntimeError(msg) from e
else:
return "Successfully wrote to clipboard."
def main() -> None:
"""Entry point for the MCP server."""
mcp.run()