Skip to content

Commit

Permalink
Screenshot demo working
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin committed Nov 30, 2024
1 parent be6e0c1 commit 627cf8a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
22 changes: 10 additions & 12 deletions examples/screenshot.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
# /// script
# dependencies = ["pyautogui"]
# dependencies = ["fastmcp", "pyautogui", "Pillow"]
# ///

"""
FastMCP Screenshot Example
A simple example that provides a tool to capture screenshots.
Give Claude a tool to capture and view screenshots.
"""

import base64
import io
import pyautogui

from fastmcp.server import FastMCP
from fastmcp import FastMCP, Image

# Create server
mcp = FastMCP("Screenshot Demo")


@mcp.tool()
def take_screenshot() -> str:
"""Take a screenshot and return it as a base64 encoded string"""
# Capture the screen
screenshot = pyautogui.screenshot()
def take_screenshot() -> Image:
"""Take a screenshot of the user's screen and return it as an image"""
import pyautogui

# Convert to base64
screenshot = pyautogui.screenshot()
buffer = io.BytesIO()
screenshot.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode()
# if the file exceeds ~1MB, it will be rejected by Claude
screenshot.convert("RGB").save(buffer, format="JPEG", quality=60, optimize=True)
return Image(data=buffer.getvalue(), format="jpeg")


if __name__ == "__main__":
Expand Down
19 changes: 15 additions & 4 deletions src/fastmcp/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def install(
typer.Option(
"--name",
"-n",
help="Custom name for the server (defaults to file name)",
help="Custom name for the server (defaults to server's name attribute or file name)",
),
] = None,
with_editable: Annotated[
Expand Down Expand Up @@ -324,17 +324,28 @@ def install(
logger.error("Claude app not found")
sys.exit(1)

# Try to import server to get its name, but fall back to file name if dependencies missing
name = server_name
if not name:
try:
server = _import_server(file, server_object)
name = server.name
except (ImportError, ModuleNotFoundError) as e:
logger.debug(
"Could not import server (likely missing dependencies), using file name",
extra={"error": str(e)},
)
name = file.stem

if claude.update_claude_config(
file,
server_name,
name,
with_editable=with_editable,
with_packages=with_packages,
force=force,
):
name = server_name or file.stem
print(f"Successfully installed {name} in Claude app")
else:
name = server_name or file.stem
print(f"Failed to install {name} in Claude app")
sys.exit(1)

Expand Down
1 change: 1 addition & 0 deletions src/fastmcp/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def to_image_content(self) -> ImageContent:
data = base64.b64encode(f.read()).decode()
else:
data = base64.b64encode(self.data).decode()

return ImageContent(type="image", data=data, mimeType=self._mime_type)


Expand Down

0 comments on commit 627cf8a

Please sign in to comment.