
M-Pesa Daraja
io.github.Mr-Macharia由 GitHub 帳號 Mr-Macharia 發布v0.1.6更新於 Oct 29, 2026
Safaricom M-Pesa Daraja tools: STK Push, C2B, B2C, B2B, QR, Ratiba. Sandbox-first, idempotent.
概覽
把 Safaricom M-Pesa Daraja API 包裝成工具,讓助手在沙箱優先的前提下收款、查詢結果並執行付款。
- 可以做什麼
- 透過 STK Push 收款,支援即發即忘、查詢狀態與等待最終結果
- 註冊 C2B 回呼網址、在沙箱模擬付款並拉取交易紀錄
- 發起 B2C、B2B、B2B Express、繳稅與沖正付款,以及 Ratiba 定期扣款與 Bill Manager 帳單
- 查詢交易狀態與帳戶餘額,並產生動態 QR 碼 PNG 檔案
- 離線查詢結果碼、正規化手機號碼、產生 STK 密碼並解析回呼內容
- 查看設定狀態並驗證憑證,且不會回傳任何密鑰
- 適用情境
- 適合讓助手在沙箱中驗證 Daraja 整合、核對與支援 M-Pesa 交易,或在受控條件下從對話中發起收款與付款。開發者與學習者也可在正式上線前,向安全的測試環境送出真實請求。
- 執行需求
- 透過 uvx 以 stdio 在本機執行(需安裝 uv),或使用已安裝的指令;僅限桌面端。需要 Daraja 應用程式的 Consumer Key 與 Secret,以及對應工具集所需的短碼、STK passkey 與發起方憑證。回呼需要公開的 HTTPS 基礎網址,可選用內建接收器;HTTP 模式需要 Bearer 權杖。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 M-Pesa Daraja,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
mpesa-daraja
Renamed from
mpesa-daraja-mcpin 0.1.5. The old package name still installs this one.
The MCP server in the M-Pesa Daraja Agent Kit. It lets an AI agent call Safaricom's Daraja API safely.
An MCP server that exposes Safaricom's Daraja APIs as tools, so an agent (Claude Code, Codex, Cursor, Gemini CLI, any MCP client) can collect payments, check whether they went through, and operate M-Pesa from a chat. It starts in the sandbox, and nothing live happens until you switch it on.
It pairs with the Agent Skills in the same kit: the skills teach an agent how to integrate M-Pesa, and this server lets it call Daraja. You can use either on its own.
Who it's for
- Developers building payment integrations: ask your agent to run a request against the sandbox and read the real response.
- Learners finding out how the Daraja API works: send real requests to a safe test environment and see what comes back.
- Businesses and teams that want an AI agent to handle payments: collecting them, checking whether they went through, and reconciling them.
Install
Let your agent do it. Paste this into Claude Code, Codex, Cursor, Gemini CLI or any coding agent:
Or do it yourself. You need uv. The wizard asks for your Daraja keys (secrets are typed hidden), checks them live with Safaricom, saves them to a file only you can read, and registers the server with Claude Code, Codex, Gemini CLI or Cursor:
Then restart your agent and ask: "Check my M-Pesa credentials, then send a KES 1 STK push to 0712345678 and tell me when it's paid." The full Agent Kit, with the skills and more examples, is in the main README.
Tools
Tool annotations mark money-moving tools as destructiveHint, so MCP clients ask for approval.
Safety
- Defaults to the sandbox.
- In production, money-moving tools refuse to run unless
DARAJA_ALLOW_LIVE_MONEY_MOVEMENT=true. - Optional guardrails, all off until set:
DARAJA_READ_ONLY=truehides every tool that moves money or writes. Use it for support bots, reconciliation and learning.DARAJA_APPROVAL_THRESHOLD=5000: B2C, B2B, tax and reversals above KES 5,000 pause for a yes/no in the chat (MCP elicitation). Clients that can't show the prompt get a refusal, and nothing is sent. Collections (STK, B2B Express, Ratiba) never prompt, because the payer enters their own PIN.DARAJA_ALLOWED_RECIPIENTS=0712345678,600000: payouts may go only to these phones and shortcodes.- For a B2C refund, pass
refund_ofwith the original M-Pesa receipt; it appears in the approval prompt and the remarks.
- Callback URLs must be HTTPS; C2B URLs with
mpesa/safaricomin them are rejected up front. - Tokens and secrets are never returned in tool output.
- Responses with
ResponseCode "0"are tagged_note: ACCEPTED for processing only, so the agent does not report them as completed payments. - Idempotent money tools. Each request is stored under an idempotency key. Repeating the key returns the stored request (
duplicate: true) instead of paying again.- Only a definitively FAILED request is re-sent.
- ACCEPTED or UNKNOWN requests (queue timeouts, network errors) are never re-sent until their result is known.
- B2C, B2B and B2B Express require an explicit
idempotency_key, e.g.refund-order-1042. Reversal, tax and Ratiba derive one from the transaction ID, the PRN and phone + name respectively. For STK the key is optional.
Configuration
Commands and configuration files
Install once with uv tool install mpesa-daraja-mcp to get the mpesa command (or prefix any command with uvx mpesa-daraja-mcp instead of mpesa):
Every data command takes --json, and mpesa --verbose … logs each HTTP request to Daraja. A failed STK Push shows the --key to retry it with. Payments use the same guardrails as the MCP tools, and approvals are asked in the terminal. uvx mpesa-daraja-mcp with no arguments is the MCP server on stdio, which is what your agent runs.
The server needs no flags or environment variables in your agent's config. Settings are read in this order, highest first:
- Environment variables, e.g. from the client's
envblock. - A project
.envin the folder the agent starts the server in, or the file named byDARAJA_DOTENV. OnlyDARAJA_*lines are read. - The user config file that
setupwrites:~/.config/mpesa-daraja-mcp/config.env(Windows:%APPDATA%\mpesa-daraja-mcp\config.env; override the folder withDARAJA_CONFIG_DIR). It is readable only by you.
The server starts even when unconfigured. Tools then explain what's missing, and mpesa_setup_status lists the gaps for each toolset without revealing any values.
To register by hand: claude mcp add --scope user mpesa -- uvx mpesa-daraja-mcp. Every other agent is covered in docs/clients.md. The mpesa-daraja and mpesa-mcp commands are aliases.
Remote access (HTTP mode)
mpesa serve --http serves MCP at /mcp and Daraja callbacks at /cb/<token>/… on one port, for n8n, cloud agents and teammates. It needs DARAJA_MCP_TOKEN, and starts read-only with every payout needing approval unless you say otherwise. Docker, HTTPS and n8n guides: https://github.com/Mr-Macharia/mpesa-daraja-skills-and-mcp/blob/main/docs/self-hosting.md
Results and callbacks
Every money or async tool returns one shape:
To let the agent see final results, enable the built-in receiver:
Callbacks are stored in DARAJA_STATE_DB:
mpesa_get_result(id)reads them.mpesa_stk_push_and_waitreturns the final outcome in one call.- Queue-timeout callbacks are recorded as UNKNOWN.
- C2B validation requests are always accepted. Use your own receiver if you need to reject payments.
Without the receiver, mpesa_stk_push_and_wait still works through STK Query. For other APIs, paste a received payload into mpesa_parse_callback.
Load only the tools you need
Every tool the server offers takes up space in your agent's context, even when it's never called. Set DARAJA_TOOLSETS to the groups you use, and the agent sees only those tools. The basic tools (core) are always on; leave the setting unset to load everything.
The groups are stk, c2b, payouts, ops, qr, ratiba and billmanager.
Use from LangChain, the OpenAI Agents SDK, Agno, Google ADK, CrewAI or your own agent
The same tools and guardrails work without a server: from mpesa_mcp.agent import mpesa_tools, or the adapters in mpesa_mcp.integrations (pip install "mpesa-daraja-mcp[langchain]", or [openai-agents], [agno], [google-adk], [crewai]). See docs/agent-frameworks.md.
Development
Tests use respx to mock Daraja. Nothing hits the network.
License
MIT © 2026 Mr-Macharia. An independent open-source project, not affiliated with or endorsed by Safaricom PLC. M-Pesa and Daraja are trademarks of Safaricom PLC.
來源:mcp-server/README.md,提交 3942fb3
工具
0版本歷史
4- v0.1.6最新Oct 29, 2026
- v0.1.4Oct 25, 2026
- v0.1.3Oct 21, 2026
- v0.1.1Oct 20, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分