Model Context Protocol (MCP)

From bibbleWiki
Jump to navigation Jump to search

Introduction

This is my first foray into MCPs which works

Clients

  • Claude
  • Claude Code
  • Cursor
  • Windsurf

Servers

Example server site is awesome-mcp-servers

Installing Claude on Ubuntu 24.04

This was reasonably painless. Goto here. When installing the instructions are

git clone https://github.com/aaddrick/claude-desktop-debian.git
cd claude-desktop-debian

# Build the package
sudo ./build-deb.sh
sudo dpkg -i ./build/electron-app/claude-desktop_0.8.0_amd64.deb

This all goes well except the version of claude is now 0.8.1 and the script in the repository is 0.8.0. Search and replace the build-deb.sh and then it fails to launch because of sandbox issues which can be fixed, in my case, with

sudo chmod 4755 /usr/local/lib/node_modules/electron/dist/chrome-sandbox

Writing a Server (Python)

To demonstrate how easy this is Matthew Berman. There is just this code and a config

from mcp.server.fastmcp import FastMCP
import time
import signal
import sys

# Handle SIGINT (Ctrl+C) gracefully
def signal_handler(sig, frame):
    print("Shutting down server gracefully...")
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Create an MCP server with increased timeout
mcp = FastMCP(
    name="count-r",
    host="127.0.0.1",
    port=5000,
    # Add this to make the server more resilient
    timeout=30  # Increase timeout to 30 seconds
)

# Define our tool
@mcp.tool()
def count_r(word: str) -> int:
    """Count the number of 'r' letters in a given word."""
    try:
        # Add robust error handling
        if not isinstance(word, str):
            return 0
        return word.lower().count("r")
    except Exception as e:
        # Return 0 on any error
        return 0

if __name__ == "__main__":
    try:
        print("Starting MCP server 'count-r' on 127.0.0.1:5000")
        # Use this approach to keep the server running
        mcp.run()
    except Exception as e:
        print(f"Error: {e}")
        # Sleep before exiting to give time for error logs
        time.sleep(5)

Build an executable as you cannot just use pip3 or pipx to install globally.

pip3 install pyinstaller
pyinstaller --onefile main.py

This produces a file in the dist directory.

Then we change the config for claude, making sure the command is appropriate to your setup. This file can be found in the following directory. ~/.config/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "count-r": {
      "command": "/home/iwiseman/dev/projects/mcpServer/pyFastMCPTest/dist/main",
      "args": [
        ""
      ],
      "host": "127.0.0.1",
      "port": 8080,
      "timeout": 30000
    }
  }
}