M-Pesa (Safaricom Daraja API) MCP server

Enables AI systems to initiate and process M-Pesa mobile payment transactions through the Safaricom Daraja API, allowing users to authorize payments directly from conversations.
Back to servers
Setup instructions
Provider
James Kanyiri
Release date
Mar 23, 2025
Language
Python
Stats
12 stars

The Daraja MCP server connects AI applications with Safaricom's Daraja API, allowing AI-driven systems to interact with M-Pesa services. This server follows the Model Context Protocol (MCP) architecture, providing tools for AI models to process payments, retrieve transaction data, and automate financial workflows through M-Pesa.

Requirements

  • Python 3.12
  • Safaricom Daraja API Credentials (Consumer Key and Secret)

Installation

Setting Up Your Environment

  1. Install uv Package Manager

    For Mac/Linux:

    curl -LsSf https://astral.sh/uv/install.sh | sh
    

    For Windows (PowerShell):

    powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
    
  2. Clone the Repository

    git clone https://github.com/jameskanyiri/DarajaMCP.git
    cd DarajaMCP
    
  3. Create and Activate a Virtual Environment

    uv venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    

    Your terminal prompt should change, indicating the virtual environment is activated.

  4. Install Dependencies

    uv sync
    

Setting up Environment Variables

  1. Copy the example environment file:

    cp .env.example .env
    
  2. Update the .env file with your actual credentials and configuration values.

    For development, use the sandbox environment. Switch to the production URL when ready.

Usage

Testing with Claude Desktop

  1. Install Claude Desktop

    • Download and install the latest version from Claude Desktop
    • Make sure you're running the latest version
  2. Configure Claude Desktop

    • Open your Claude Desktop configuration file:

      # On MacOS/Linux
      code ~/Library/Application\ Support/Claude/claude_desktop_config.json
      
      # On Windows
      code %APPDATA%\Claude\claude_desktop_config.json
      
    • Create the file if it doesn't exist

  3. Add Server Configuration

    Choose one of the following configurations:

    Anthropic's Recommended Format

    {
      "mcpServers": {
        "daraja": {
          "command": "uv",
          "args": [
            "--directory",
            "/ABSOLUTE/PATH/TO/PARENT/FOLDER/DarajaMCP",
            "run",
            "main.py"
          ]
        }
      }
    }
    

    Working Configuration (Tested)

    {
      "mcpServers": {
        "DarajaMCP": {
          "command": "/ABSOLUTE/PATH/TO/PARENT/.local/bin/uv",
          "args": [
            "--directory",
            "/ABSOLUTE/PATH/TO/PARENT/FOLDER/DarajaMCP",
            "run",
            "main.py"
          ]
        }
      }
    }
    

    To find the full path to uv, run:

    # On MacOS/Linux
    which uv
    
    # On Windows
    where uv
    
  4. Verify Configuration

    • Save the configuration file
    • Restart Claude Desktop
    • Look for the hammer 🔨 icon in the interface
    • Click it to see the available tools:
      • generate_access_token
      • stk_push (Future Implementation)
      • query_transaction_status (Future Implementation)
      • b2c_payment (Future Implementation)
      • account_balance (Future Implementation)

Available Tools

Payment Tools

stk_push

Initiate an M-Pesa STK push request to prompt the customer to authorize a payment.

Inputs:

  • amount (int): The amount to be paid
  • phone_number (int): The phone number of the customer

Returns: JSON formatted M-PESA API response

generate_qr_code

Generate a QR code for a payment request that customers can scan.

Inputs:

  • merchant_name (str): Name of the company/M-Pesa Merchant Name
  • transaction_reference_no (str): Transaction reference number
  • amount (int): The total amount for the transaction
  • transaction_type (Literal["BG", "WA", "PB", "SM", "SB"]): Transaction type
  • credit_party_identifier (str): Credit Party Identifier (Mobile Number, Business Number, etc.)

Returns: JSON formatted M-PESA API response with QR code data

Document Processing Tools

create_source

Create a connector from data source to unstructured server for processing.

Inputs:

  • connector_name (str): The name of the source connector to create

Returns: Source connector details including name and ID

create_destination

Create a connector from unstructured server to destination for data storage.

Inputs:

  • connector_name (str): The name of the destination connector to create

Returns: Destination connector details including name and ID

create_workflow

Create a workflow to process data from source connector to destination connector.

Inputs:

  • workflow_name (str): The name of the workflow to create
  • source_id (str): The ID of the source connector
  • destination_id (str): The ID of the destination connector

Returns: Workflow details including name, ID, status, type, sources, destinations, and schedule

run_workflow

Execute a workflow.

Inputs:

  • workflow_id (str): The ID of the workflow to run

Returns: Workflow execution status

get_workflow_details

Get detailed information about a workflow.

Inputs:

  • workflow_id (str): The ID of the workflow to get details

Returns: Workflow details including name, ID, and status

fetch_documents

Fetch documents analyzed during workflow execution.

Inputs: None

Returns: List of analyzed documents

Available Prompts

Payment Prompts

stk_push_prompt

Generate a prompt for initiating an M-Pesa STK push payment request.

Inputs:

  • phone_number (str): The phone number of the customer
  • amount (int): The amount to be paid
  • purpose (str): The purpose of the payment

Returns: Formatted prompt string for STK push request

generate_qr_code_prompt

Generate a prompt for creating an M-Pesa QR code payment request.

Inputs:

  • merchant_name (str): Name of the merchant/business
  • amount (int): Amount to be paid
  • transaction_type (str): Type of transaction
  • identifier (str): The recipient identifier
  • reference (str, optional): Transaction reference number

Returns: Formatted prompt string for QR code generation

Document Processing Prompts

create_and_run_workflow_prompt

Generate a prompt to create and run a workflow for document processing.

Inputs:

  • user_input (str): The user's processing requirements

Returns: Formatted prompt for workflow creation and execution

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "daraja" '{"command":"uv","args":["--directory","/ABSOLUTE/PATH/TO/PARENT/FOLDER/DarajaMCP","run","main.py"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "daraja": {
            "command": "uv",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/PARENT/FOLDER/DarajaMCP",
                "run",
                "main.py"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "daraja": {
            "command": "uv",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/PARENT/FOLDER/DarajaMCP",
                "run",
                "main.py"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later