Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run #7

Open
itszn opened this issue Dec 11, 2024 · 6 comments
Open

How to run #7

itszn opened this issue Dec 11, 2024 · 6 comments

Comments

@itszn
Copy link
Collaborator

itszn commented Dec 11, 2024

Running directly

  • Need uv

Install libs with uv or pip
ie:

pip install flask anthropic requests python-dotenv

Set anthoripc key env: export ANTHROPIC_API_KEY=keyhere

Go into server-a and python server.py

In another term go into server-b and python server.py

Test to see if the gateway can start: uv run path/to/gateway/service.py, but you have to update the path to be your local

Now configure your claude desktop to run the gateway script with uv

{
    "mcpServers": {
      "gateway": {
        "command": "uv",
        "args": ["run", "path/to/gateway/service.py"]
      }
   }
}

Now claude desktop should be able to use the gateway MCP tool on restart, hopefully it all works


Helper notes:

https://docs.astral.sh/uv/getting-started/installation/

uv run /Users/dazzagreenwood/mcp-agent-router/gateway-agent/service.py

@dazzaji
Copy link
Owner

dazzaji commented Dec 11, 2024

FOR VIRTUAL ENVIRONMENT

Creating a virtual environment in Python on VS Code on a Mac involves the following steps:


Step 1: Open Terminal or Integrated Terminal in VS Code

  1. Open VS Code.
  2. Open the terminal by navigating to View > Terminal or pressing Ctrl + (backtick).

Step 2: Navigate to Your Project Directory

  1. Use the cd command to navigate to your project directory:
    cd /path/to/your/project

Step 3: Create the Virtual Environment

  1. Use the following command to create a virtual environment (replace venv with your preferred environment name):

    python3 -m venv venv
    • Note: Ensure you have Python 3 installed. You can check with:
      python3 --version

Step 4: Activate the Virtual Environment

  1. Activate the virtual environment with this command:

    source venv/bin/activate
    • Once activated, you should see (venv) at the start of your terminal prompt.

Step 5: Install Extensions in VS Code (if necessary)

  1. Install the Python extension for VS Code (if not already installed):
    • Go to the Extensions view (Ctrl + Shift + X or Cmd + Shift + X on Mac).
    • Search for "Python" and install the extension by Microsoft.

Step 6: Configure Python Interpreter

  1. In VS Code, press Cmd + Shift + P to open the Command Palette.
  2. Type and select Python: Select Interpreter.
  3. Choose the interpreter from your virtual environment. It should look like this:
    /path/to/your/project/venv/bin/python
    

Step 7: Verify the Virtual Environment

  1. Check if the correct Python interpreter is being used by running:

    which python

    It should point to the venv/bin/python path.

  2. Optionally, verify installed packages inside the virtual environment with:

    pip list

Step 8: Deactivate the Virtual Environment (Optional)

When you're done working in the environment, deactivate it by running:

deactivate

Step 9: Automate Activation in Terminal (Optional)

If you want VS Code to automatically activate the virtual environment when you open the project:

  1. Create a .vscode folder in your project directory.
  2. Add a settings.json file inside .vscode with the following content:
    {
        "python.pythonPath": "venv/bin/python",
        "terminal.integrated.env.osx": {
            "VIRTUAL_ENV": "venv",
            "PATH": "${workspaceFolder}/venv/bin:${env:PATH}"
        }
    }

Now, the environment will automatically activate when you open the terminal in VS Code.

@dazzaji
Copy link
Owner

dazzaji commented Dec 11, 2024

Cleaned up note on running the project:

Here's how to set up and run your MCP project with the specifics you provided, combining your rough notes and project structure:


Step 1: Set Up a Virtual Environment

  1. Navigate to the Project Directory:

    cd /path/to/mcp-agent-router
  2. Create the Virtual Environment:

    /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 -m venv .venv
  3. Activate the Virtual Environment:

    source .venv/bin/activate

3.5. Select the right Python interpreter

  1. Install Required Libraries:
    pip install flask anthropic requests python-dotenv uv

Step 2: Set Environment Variables

Export the ANTHROPIC_API_KEY as required by the project:

export ANTHROPIC_API_KEY="your_api_key_here"

Step 3: Run Server A

  1. Navigate to server-a:

    cd server-a
  2. Run the Server:

    python server.py

Step 4: Run Server B

  1. Open a New Terminal and Activate the Virtual Environment:

    source /path/to/mcp-agent-router/.venv/bin/activate
  2. Navigate to server-b:

    cd server-b
  3. Run the Server:

    python server.py

Step 5: Start the Gateway

  1. Open Another Terminal and Activate the Virtual Environment:

    source /path/to/mcp-agent-router/.venv/bin/activate
  2. Run the Gateway Using uv:
    Replace path/to/gateway/service.py with the actual path to the gateway-agent/service.py file:

    uv run /path/to/mcp-agent-router/gateway-agent/service.py

Step 6: Configure Claude Desktop

  1. Locate Your Claude Desktop Config File:
    This is usually named something like claude_desktop_config.json.

  2. Edit the Config File:
    Add the following configuration under "mcpServers":

    {
      "mcpServers": {
        "gateway": {
          "command": "uv",
          "args": ["run", "/Users/dazzagreenwood/mcp-agent-router/gateway-agent/service.py"]
        }
      }
    }
  3. Restart Claude Desktop:
    Quit and restart the application to apply the changes.


Step 7: Verify the Setup

  1. Check Server Logs:
    Ensure that both server-a and server-b are running without errors.

  2. Test Claude Desktop:
    Interact with the MCP Gateway via Claude Desktop to verify that the client-server communication works as expected.


Troubleshooting

  • If the gateway fails to start, ensure uv is correctly installed:
    pip install uv
  • If Claude Desktop cannot connect to the gateway, double-check the file path in the claude_desktop_config.json.

Let me know if you need further clarifications!

@dazzaji
Copy link
Owner

dazzaji commented Dec 11, 2024

Even MORE Complete Cheat Sheet for Running This Project

Certainly! Below is a comprehensive cheat-sheet capturing all the steps needed to set up and run the mcp-agent-router project successfully, taking into account everything we learned during troubleshooting:


MCP-Agent-Router Project Setup and Execution Cheat-Sheet

1. Initial Setup

Step 1: Clone the Repository

If you haven’t already cloned the project:

git clone <repository-url>
cd mcp-agent-router

Step 2: Set Up the Python Virtual Environment

  1. Remove any conflicting virtual environments if they exist:

    rm -rf .venv venv
  2. Create a new virtual environment using Python 3.11:

    /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 -m venv ./venv
  3. Link venv to .venv to satisfy tools like uv:

    ln -s venv .venv
  4. Activate the virtual environment:

    source ./venv/bin/activate
  5. Confirm the Python version:

    python --version

    The output should be:

    Python 3.11.1
    
  6. Install the required dependencies:

    pip install flask anthropic requests python-dotenv uv mcp

Step 3: Configure .python-version

If the project contains a .python-version file, ensure it matches Python 3.11:

echo "3.11" > .python-version

2. Running the Project

Step 4: Start Server A

  1. Open a new terminal tab or window in VS Code.
  2. Activate the virtual environment:
    source ./venv/bin/activate
  3. Navigate to the server-a directory:
    cd server-a
  4. Run the server:
    python server.py

Step 5: Start Server B

  1. Open another terminal tab or window.
  2. Activate the virtual environment:
    source ./venv/bin/activate
  3. Navigate to the server-b directory:
    cd server-b
  4. Run the server:
    python server.py

Step 6: Start the Gateway

  1. Open another terminal tab or window.
  2. Ensure the virtual environment is active:
    source ./venv/bin/activate
  3. Run the gateway:
    uv run /Users/dazzagreenwood/mcp-agent-router/gateway-agent/service.py

3. Testing and Verification

Step 7: Verify the Setup

  1. Check Each Server:

    • Confirm that server-a, server-b, and the gateway server are running without errors.
    • Each server should be displaying logs or listening for requests.
  2. Test with the Client (if applicable):
    If the project includes a client like Claude Desktop, connect it to the gateway:

    • Edit the claude_desktop_config.json file to include:
      {
        "mcpServers": {
          "gateway": {
            "command": "uv",
            "args": ["run", "/Users/dazzagreenwood/mcp-agent-router/gateway-agent/service.py"]
          }
        }
      }
    • Restart Claude Desktop.
    • Test the client by interacting with the connected servers.

4. Troubleshooting Tips

If uv Uses the Wrong Python Version:

  1. Ensure .venv is linked to venv:
    ln -s venv .venv
  2. Ensure the VIRTUAL_ENV variable points to the correct environment:
    export VIRTUAL_ENV=$(pwd)/venv

If mcp Module is Not Found:

  1. Confirm mcp is installed:
    pip list | grep mcp
  2. Reinstall it if necessary:
    pip install mcp

If VS Code is Misaligned:

  1. Open Command Palette (Cmd + Shift + P).
  2. Select Python: Select Interpreter and choose Python 3.11.1 ('venv').

If Gateway or Servers Fail to Start:

  1. Check the logs in the terminal for detailed error messages.
  2. Ensure all dependencies are installed in the active virtual environment.
  3. Manually run the failing script with Python to isolate issues:
    python /path/to/script.py

5. Cleanup and Restart

To restart the entire project setup from scratch:

  1. Stop all servers (Ctrl+C in each terminal).
  2. Deactivate the virtual environment:
    deactivate
  3. Delete existing virtual environments and recreate them following Step 2.

This cheat-sheet should serve as a reliable guide for running your mcp-agent-router project, including all the tricky steps we encountered during the setup. Let me know if there’s anything else you’d like added!

@dazzaji
Copy link
Owner

dazzaji commented Dec 11, 2024

Best Cold-Start "How to Run" Documentation

(Based on continuing periodic issues, I've further documented below how best to run and use this project based on the various obstacles that have popped up. Note that the file paths should be updated to your machine and I hard-coded the version of Python I'm using to deal with countless issues pointing to the right version and maintaining the link to it.)

Okay, let's get the mcp-agent-router project running with Claude Desktop and the MCP Inspector. Here's a step-by-step guide that addresses your specific concerns, using uv as requested and focusing on understanding how to integrate Claude Desktop and use the Inspector for troubleshooting.

1. Project Setup with uv

First, ensure your project environment is correctly set up with uv.

# Clone the repository if you haven't already
git clone <repository-url>
cd mcp-agent-router

# Remove existing virtual environments if any
rm -rf .venv venv

# Create a new virtual environment with Python 3.11
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 -m venv ./venv

# Link venv to .venv for uv
ln -s venv .venv

# Activate the virtual environment
source ./venv/bin/activate

# Confirm Python version
python --version
# Expected output is: Python 3.11.x

# Install necessary dependencies using uv
uv pip install flask anthropic requests python-dotenv mcp

# Update the python version
echo "3.11" > .python-version

Explanation:

  • We're creating the virtual environment using Python 3.11, which you specified in the .python-version file.
  • The symbolic link ln -s venv .venv is used to make uv correctly recognize the virtual environment.
  • uv pip install installs required packages.

2. Start the MCP Servers and Gateway Agent

Next, we'll start each of your three "black box" servers and the gateway agent in separate terminals.

Terminal 1: Server A (Personal Trainer)

cd server-a
source ../venv/bin/activate
python server.py

Terminal 2: Server B (Work Assistant)

cd ../server-b
source ../venv/bin/activate
python server.py

Terminal 3: Gateway Agent

cd ../gateway-agent
source ../venv/bin/activate
 uv run service.py

Explanation:

  • Each server and the gateway agent are started in their respective directories within your project.
  • We use source ../venv/bin/activate to ensure each uses the same virtual environment.
  • This step assumes that servers A and B have a server.py file with basic MCP server implementation. It also assumes the gateway-agent has a service.py file to run with uv.

3. Configure Claude Desktop

Now, let's configure Claude Desktop to recognize your gateway agent and servers. Here’s how your claude_desktop_config.json should be structured, assuming server-a, server-b, and the gateway-agent are on ports 5010, 5001 and 8000 respectively:

{
   "mcpServers": {
      "gateway-agent": {
         "url": "http://localhost:8000/mcp/v1"
      },
      "server-a": {
        "url": "http://localhost:5010/mcp/v1"
      },
      "server-b": {
         "url": "http://localhost:5001/mcp/v1"
      }
   }
}

Explanation:

  • Locate the claude_desktop_config.json file in your system (its path can vary depending on your OS). Refer to the MCP docs for OS specific location of the file.
  • The mcpServers object now contains configurations for each server, including the gateway-agent, server-a and server-b.. Make sure these URL paths match the actual endpoints you have set up in the corresponding server.py files.
    * The gateway is explicitly using http (not https) protocol for the Inspector and Claude integration.
  • Restart Claude Desktop after updating this file for the changes to take effect.

4. Starting and Using the MCP Inspector

Now we can run and use the inspector to test out the servers and debug.

Open a new terminal (Terminal 4):

cd mcp-agent-router/gateway-agent
source ../venv/bin/activate
npx @modelcontextprotocol/inspector http://localhost:8000/mcp/v1

Explanation:
* We open a new terminal in mcp-agent-router/gateway-agent directory.
* We activate the same environment as for the gateway server.

  • The command starts the MCP Inspector and attempts to connect directly to your gateway agent's HTTP endpoint. The inspector UI will open in your default browser.

5. Interacting with MCP Inspector

  • Verify connection:
  • Once the Inspector UI loads, you will see three fields for Command, Arguments and Environment Variables. If the client was able to connect to the server, the box with the server name (e.g. gateway-agent) will show connected. If it fails to connect, it will say 'failed'. You may need to click the Format button in the 'Commands' section to get the servers to connect.
  • Examine available tools:
    • Click on the Tools tab to see tools your gateway agent offers (e.g., route_task). If the connection is made successfully, you will see that a tool called route_task is now available in the inspector.
  • Test routing:
    • Use the Inspector to call the route_task tool with various user_input queries. For example:
      • {"user_input": "How much should I weigh?"} (should go to server A)
      • {"user_input": "What is my next project deadline?"} (should go to server B)
    • Observe the responses and check terminal output for which server was called. You can also look in your server log files in the /shared directory.
  • Examine all communication: You can see all communication between the Client and the server as it happens, including JSON messages that the client is sending to the server and what the server is sending back to the client.

Explanation:

  • The MCP Inspector UI lets you interact with the server and inspect the messages sent and received.
  • By using Inspector to test the routing, you can verify that the route_task tool is functioning correctly as you have described it.

6. Interacting with Claude Desktop

After completing these steps, Claude Desktop should be able to send queries through your gateway server.

To test with Claude Desktop:

  • Ensure you've restarted Claude Desktop after changing claude_desktop_config.json.
  • In Claude Desktop, try entering prompts that correspond to tools provided by your servers:
    • For example, if you have configured a tool on server-a to get health information, entering "how much should I weigh?" should be routed to server-a. The response should come through your gateway-agent, then to Claude Desktop, and be displayed.
    • Similarly, a query like "what are my deadlines this week" should go to server-b
    • The response from the servers will be displayed in Claude's conversation history.

Troubleshooting:

  • If uv is using wrong Python Version: Follow the tips in the "cheat-sheet" you provided, specifically ensuring the .venv link is correct, and that the VIRTUAL_ENV variable is set correctly, and that VS code is picking the correct virtual environment.
  • If mcp module is not found: Again, as in your "cheat sheet" check if the mcp module is installed using pip list | grep mcp. Reinstall it using pip install mcp if necessary.
  • If Servers or Gateway fail to Start:
    • Look at console logs for error messages in each terminal.
    • Check that all dependencies are installed in the active virtual environments.
    • Manually run each failing script in its own terminal with python /path/to/script.py to isolate issues.

Key Points to Understand

  • Virtual Environments are Crucial: Make sure each terminal uses the virtual environment created, as described in Step 2.
  • Port and URL Consistency: Ensure the port numbers in your claude_desktop_config.json match the actual ports your servers are running on and that the URL paths /mcp/v1 are consistent for each server.
  • uv for Package Management: This guide uses uv for dependency and server execution as you specified, be sure to use that in all cases for maximum compatibility.
  • Inspector for Visibility: The MCP Inspector is very useful for examining the messages being sent and received, this can help you pinpoint issues in routing, or data handling. Be sure to use the Inspector UI as described in Step 5.
  • Claude for Testing: Claude Desktop's MCP support allows for real-world end-to-end testing of the various tools you are creating, using the client as you would in a user scenario.
  • Logging: Ensure that your server and gateway agent implementations use Python’s logging module, outputting to stderr so you can check logs for errors and information in the terminal.

Note: Be sure to check the log files that are output into the shared folder for debugging. Also use the Inspector to make sure that you have each server connecting and correctly routing messages as designed.

@dazzaji
Copy link
Owner

dazzaji commented Dec 11, 2024

I think there are still some issues with the documentation on this! Here is another shot at correcting the causes of the errors:

Run the Project

Okay, let's get your mcp-agent-router project running with Claude Desktop and the MCP Inspector. Here's a detailed step-by-step guide that uses uv as requested, focuses on understanding how to integrate Claude Desktop, and shows how to use the Inspector for troubleshooting. We will treat each of the three servers as "black boxes" so we can see how MCP interoperability between servers from different teams or even companies would be.

1. Project Setup with uv

First, ensure your project environment is correctly set up with uv.

# Clone the repository if you haven't already
git clone <repository-url>
cd mcp-agent-router

# Remove existing virtual environments if any
rm -rf .venv venv

# Create a new virtual environment with Python 3.11 (or match with .python-version)
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 -m venv ./venv

# Link venv to .venv for uv
ln -s venv .venv

# Activate the virtual environment
source ./venv/bin/activate

# Confirm Python version
python --version
# Expected output is: Python 3.11.x

# Install necessary dependencies using uv
uv pip install flask anthropic requests python-dotenv mcp

Explanation:

  • We're creating the virtual environment using Python 3.11, matching the version specified in the .python-version file. If you have a different Python 3.11 version installed, please use that.
  • The symbolic link ln -s venv .venv is used to make uv correctly recognize the virtual environment.
  • uv pip install installs required packages including flask, anthropic, requests, python-dotenv and mcp.

2. Prepare Server Configuration Files

Create .env files in the server-a and server-b directories and include your Anthropic API Key:

  • server-a/.env:

    ANTHROPIC_API_KEY=your_anthropic_api_key
    
  • server-b/.env:

    ANTHROPIC_API_KEY=your_anthropic_api_key
    

Explanation:

  • The server A and server B files use the python-dotenv to automatically pull the keys from these .env files.

3. Start the MCP Servers and Gateway Agent

Next, we'll start each of your three "black box" servers and the gateway agent in separate terminals.

Terminal 1: Server A (Personal Trainer)

cd server-a
source ../venv/bin/activate
uv run server.py

Make note of the fact that server A runs on port 5010.

Terminal 2: Server B (Work Assistant)

cd ../server-b
source ../venv/bin/activate
uv run server.py

Make note of the fact that server B runs on port 5001.

Terminal 3: Gateway Agent

cd ../gateway-agent
source ../venv/bin/activate
 uv run service.py

Make note of the fact that gateway agent will listen on port 8000.

Explanation:

  • Each server and the gateway agent are started in their respective directories within your project.
  • We use source ../venv/bin/activate to ensure each uses the same virtual environment, and we also use uv run instead of python which ensures that the python that is being used is the one defined for this project.
  • This step assumes that servers A and B have a server.py file with basic MCP server implementation. It also assumes the gateway-agent has a service.py file to run with uv.
  • NOTE: The gateway agent uses a http (not https) endpoint at /mcp/v1 by default.

4. Configure Claude Desktop

Now, let's configure Claude Desktop to recognize your gateway agent and servers. Here’s how your claude_desktop_config.json should be structured, assuming server-a, server-b, and the gateway-agent are on ports 5010, 5001 and 8000 respectively:

{
  "mcpServers": {
    "gateway-agent": {
      "command": "uv",
      "args": ["run", "/Users/dazzagreenwood/mcp-agent-router/gateway-agent/service.py"],
      "url": "http://localhost:8000/mcp/v1"
    },
       "server-a": {
        "url": "http://localhost:5010/mcp/v1"
      },
       "server-b": {
         "url": "http://localhost:5001/mcp/v1"
      }
  }
}

Replace /Users/dazzagreenwood/mcp-agent-router/gateway-agent/service.py with the correct path to your service.py file.

Explanation:

  • Locate the claude_desktop_config.json file in your system (its path can vary depending on your OS). Refer to the MCP docs for OS specific location of the file.
  • The mcpServers object now contains configurations for each server, including the gateway-agent, server-a and server-b.
    * Make sure that the "url" for the gateway-agent matches the address where your gateway agent is running, including http:// not https://.
    * Be sure the URLs match the actual port values that server a and server b are running on, which are 5010 and 5001 respectively.
  • Restart Claude Desktop after updating this file for the changes to take effect.

5. Starting and Using the MCP Inspector

Now we can run and use the inspector to test out the servers and debug.

Open a new terminal (Terminal 4):

cd mcp-agent-router/gateway-agent
source ../venv/bin/activate
npx @modelcontextprotocol/inspector http://localhost:8000/mcp/v1

Explanation:
* We open a new terminal in mcp-agent-router/gateway-agent directory.
* We activate the same environment as for the gateway server.

  • The command starts the MCP Inspector and attempts to connect directly to your gateway agent's HTTP endpoint. The inspector UI will open in your default browser.

6. Interacting with MCP Inspector

  • Verify connection:
  • Once the Inspector UI loads, you will see three fields for Command, Arguments and Environment Variables. If the client was able to connect to the server, the box with the server name (e.g. gateway-agent) will show connected. If it fails to connect, it will say 'failed'. You may need to click the Format button in the 'Commands' section to get each server to connect.
  • Examine available tools:
    • Click on the Tools tab to see tools your gateway agent offers (e.g., route_task). If the connection is made successfully, you will see that a tool called route_task is now available in the inspector.
  • Test routing:
    • Use the Inspector to call the route_task tool with various user_input queries. For example:
      • {"user_input": "How much should I weigh?"} (should go to server A)
      • {"user_input": "What is my next project deadline?"} (should go to server B)
    • Observe the responses and check terminal output for which server was called. You can also look in your server log files in the /shared directory.
  • Examine all communication: You can see all communication between the Client and the server as it happens, including JSON messages that the client is sending to the server and what the server is sending back to the client.

Explanation:

  • The MCP Inspector UI lets you interact with the server and inspect the messages sent and received.
  • By using Inspector to test the routing, you can verify that the route_task tool is functioning correctly as you have described it.
  • If the connections do not show up, you may need to "format" the connection in the Commands section of Inspector, by clicking on the button next to the Command field. This will ensure that inspector is correctly making the initial connections. If your server is running with debug=True you should see detailed output in the terminal.

7. Interacting with Claude Desktop

After completing these steps, Claude Desktop should be able to send queries through your gateway server.

To test with Claude Desktop:

  • Ensure you've restarted Claude Desktop after changing claude_desktop_config.json.
  • In Claude Desktop, try entering prompts that correspond to tools provided by your servers:
    • For example, if you have configured a tool on server-a to get health information, entering "how much should I weigh?" should be routed to server-a. The response should come through your gateway-agent, then to Claude Desktop, and be displayed.
    • Similarly, a query like "what are my deadlines this week" should go to server-b
    • The response from the servers will be displayed in Claude's conversation history.

Troubleshooting:

  • If uv is using wrong Python Version: Follow the tips in the "cheat-sheet" you provided, specifically ensuring the .venv link is correct, and that the VIRTUAL_ENV variable is set correctly, and that VS code is picking the correct virtual environment.
  • If mcp module is not found: Again, as in your "cheat sheet" check if the mcp module is installed using pip list | grep mcp. Reinstall it using pip install mcp if necessary.
  • If Servers or Gateway fail to Start:
    • Look at console logs for error messages in each terminal.
    • Check that all dependencies are installed in the active virtual environments.
    • Manually run each failing script in its own terminal with python /path/to/script.py to isolate issues.
    • Make sure the ports specified in the config file match the ports each server is running on.

Key Points to Understand

  • Virtual Environments are Crucial: Make sure each terminal uses the virtual environment created, as described in Step 2.
  • Port and URL Consistency: Ensure the port numbers in your claude_desktop_config.json match the actual ports your servers are running on and that the URL paths /mcp/v1 are consistent for each server.
  • uv for Package Management: This guide uses uv for dependency and server execution as you specified, be sure to use that in all cases for maximum compatibility.
  • Inspector for Visibility: The MCP Inspector is very useful for examining the messages being sent and received, this can help you pinpoint issues in routing, or data handling. Be sure to use the Inspector UI as described in Step 5.
  • Claude for Testing: Claude Desktop's MCP support allows for real-world end-to-end testing of the various tools you are creating, using the client as you would in a user scenario.
  • Logging: Ensure that your server and gateway agent implementations use Python’s logging module, outputting to stderr so you can check logs for errors and information in the terminal.

Let me know if you need further help, or run into any issues. Be sure to check the log files that are output into the shared folder for debugging. Also use the Inspector to make sure that you have each server connecting and correctly routing messages as designed.

Explanation of Changes

  1. Corrected MCP Inspector usage:
    • The Inspector now connects directly to the gateway agent, making the necessary calls to determine supported tools and other functionality.
  2. Fixed claude_desktop_config.json:
    • The config file is now complete and correct, including command, and args, so that Claude Desktop will activate the service.py file of the gateway agent.
    • Added explict URLs for server a and b.
    • Added explicit instruction to restart Claude Desktop.
  3. Explained uv usage:
    • Instructions now include use of uv for the virtual environment, dependency installation, and code execution
    • Instructions include how to set the Python version with .python-version.
  4. Clarified Gateway Agent Implementation
    • The gateway agent is a small routing application that calls the downstream server as a client.
    • The routing is done with a basic keyword matching routine (which can be greatly expanded in subsequent iterations)
  5. Added explicit information on which servers are available on which ports: This will help you to ensure that the config file is correct.
  6. Emphasized debug mode: The servers may need to be run with the debug flag set to True, to make sure errors will be displayed on the server side.
  7. Provided a more accurate and comprehensive troubleshoot section
  8. Removed unnecessary information from initial prompts.

@dazzaji
Copy link
Owner

dazzaji commented Dec 12, 2024

One more time, with feeling!


Running the mcp-agent-router project with Claude Desktop and MCP Inspector

Here's a step-by-step guide to setting up and running the mcp-agent-router project, configuring Claude Desktop, and using the MCP Inspector for troubleshooting.

1. Project Setup with uv

  • Clone the repository:

    git clone <repository-url>
    cd mcp-agent-router
  • Create and activate a virtual environment: (This example uses Python 3.11, as specified in the project's .python-version)

    python3.11 -m venv .venv
    source .venv/bin/activate
  • Install uv:

    pip install uv
  • Install dependencies:

    uv pip install -r pyproject.toml

2. Configure Environment Variables

  • Create .env files in the server-a and server-b directories with the following content:

    server-a/.env:

    ANTHROPIC_API_KEY=your_anthropic_api_key
    

    server-b/.env:

    ANTHROPIC_API_KEY=your_anthropic_api_key
    
  • Replace your_anthropic_api_key with your actual Anthropic API key.

3. Start the MCP Servers and Gateway Agent

Each of your servers (server-a, server-b, and gateway-agent) will run as independent processes. You'll need to start each one in a separate terminal.

  • Terminal 1: Server A (Personal Trainer)

    cd server-a
    source ../.venv/bin/activate
    uv run server.py
  • Terminal 2: Server B (Work Assistant)

    cd ../server-b
    source ../.venv/bin/activate
    uv run server.py
  • Terminal 3: Gateway Agent

    cd ../gateway-agent
    source ../.venv/bin/activate
    uv run service.py

Explanation:

  • Each server is started using uv run <script_name>. This ensures that the correct virtual environment is used.
  • server-a and server-b are started with uv run server.py.
  • The gateway-agent is started with uv run service.py.
  • The gateway agent (service.py) is currently a basic implementation and does not contain robust routing logic. It will need to be modified to correctly route requests to server-a and server-b based on the user's input.

4. Configure Claude Desktop

  • Locate your claude_desktop_config.json file. The location depends on your operating system. Refer to the official MCP documentation for the exact path.
  • Add the following configuration under the mcpServers key:
{
  "mcpServers": {
    "gateway-agent": {
      "command": "uv",
      "args": ["run", "/Users/yourusername/mcp-agent-router/gateway-agent/service.py"],
      "cwd": "/Users/yourusername/mcp-agent-router/gateway-agent",
      "env": {}
    }
  }
}

Explanation:

  • Replace /Users/yourusername/mcp-agent-router/gateway-agent/service.py with the absolute path to your service.py file.
  • The cwd field specifies the working directory for the command, ensuring that uv is executed in the correct context.
  • Restart Claude Desktop for the changes to take effect.

5. Running and Using the MCP Inspector

The MCP Inspector allows you to test and debug your servers.

  • Open a new terminal (Terminal 4):

    cd mcp-agent-router/gateway-agent
    source ../.venv/bin/activate
    npx @modelcontextprotocol/inspector http://localhost:8000/mcp/v1

    This command starts the MCP Inspector and connects it to your gateway agent's HTTP endpoint.

  • Interact with the Inspector:

    1. Open the Inspector UI: The npx command will provide a URL to the Inspector UI. Open this URL in your browser.
    2. Verify connection: Once the Inspector UI loads, you should see your gateway agent listed and connected.
    3. Examine available tools: Click on the "Tools" tab to see the tools offered by your gateway agent (e.g., route_task).
    4. Test routing: Use the Inspector to call the route_task tool with various user_input queries. For example:
      • {"user_input": "How much should I weigh?"} (should go to server A)
      • {"user_input": "What is my next project deadline?"} (should go to server B)
    5. Observe the responses: Check the terminal output of your servers to verify which server was called. Inspect the "Request" and "Response" sections in the Inspector to see the communication flow.

Explanation:

  • The MCP Inspector acts as a proxy between Claude Desktop and your MCP servers, allowing you to inspect and modify the messages being exchanged.
  • By testing different inputs and observing the behavior, you can verify that your routing logic and server implementations are working correctly.

6. Interacting with Claude Desktop

After completing these steps, Claude Desktop should be able to send queries through your gateway server.

To test with Claude Desktop:

  • Ensure you've restarted Claude Desktop after changing claude_desktop_config.json.
  • In Claude Desktop, try entering prompts that correspond to tools provided by your servers:
    • For example, if you've configured server-a to handle health-related queries, entering "How much should I weigh?" should be routed to server-a.
    • Similarly, a query like "What are my deadlines this week?" should go to server-b.
  • The response from the servers will be displayed in Claude's conversation history.

Troubleshooting:

  • If uv is using wrong Python Version: Follow the tips in the "cheat-sheet" you provided, specifically ensuring the .venv link is correct, and that the VIRTUAL_ENV variable is set correctly, and that VS Code is picking the correct virtual environment.
  • If mcp module is not found: Check if the mcp module is installed using pip list | grep mcp. Reinstall it using pip install mcp if necessary.
  • If Servers or Gateway fail to Start:
    • Look at console logs for error messages in each terminal. Make sure each server is started in its own terminal.
    • Check that all dependencies are installed in the active virtual environments.
    • Manually run each failing script in its own terminal with python /path/to/script.py to isolate issues.
    • Make sure the ports specified in the config file match the ports each server is running on.
    • Ensure the gateway agent's url is http://localhost:8000/mcp/v1 (or the correct port if you have overridden this).

Key Points to Understand

  • Virtual Environments are Crucial: Make sure each terminal uses the virtual environment created, as described in Step 2.

  • Port and URL Consistency: Ensure the port numbers in your claude_desktop_config.json match the actual ports your servers are running on.

  • uv for Package Management: This guide uses uv for dependency and server execution as you specified, be sure to use that in all cases for maximum compatibility.

  • Inspector for Visibility: The MCP Inspector is very useful for examining the messages being sent and received, this can help you pinpoint issues in routing, or data handling. Be sure to use the Inspector UI as described in Step 5.

  • Claude for Testing: Claude Desktop's MCP support allows for real-world end-to-end testing of the various tools you are creating, using the client as you would in a user scenario.

  • Logging: Ensure that your server and gateway agent implementations use Python’s logging module, outputting to stderr so you can check logs for errors and information in the terminal.

  • The gateway agent service.py is just a placeholder for basic routing. In order to properly implement the routing logic, you'll need to modify the service.py file, using more advanced techniques, like NLP, or more specific mapping of inputs to particular server endpoints.

  1. Removed Inaccuracies: Corrected misleading statements about uv add, server start commands, and the completeness of the provided code.
  2. Added README Structure: Used clear headings and subheadings to organize the information logically.
  3. Included Key Points: Summarized important considerations for understanding and working with the project.

This revised response provides a more accurate, complete, and user-friendly guide for setting up and running the mcp-agent-router project. It addresses the issues identified in the original responses and clarifies the role of each component, while also offering more specific guidance on using the MCP Inspector and Claude Desktop for testing and debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants