Stripe MCP agent

Using the Stripe MCP server to create an Agent that can interact with the Stripe API:

1"""💵 Stripe MCP Agent - Manage Your Stripe Operations
2
3This example demonstrates how to create an Kern agent that interacts with the Stripe API via the Model Context Protocol (MCP). This agent can create and manage Stripe objects like customers, products, prices, and payment links using natural language commands.
4
5
6Setup:
72. Install Python dependencies: `uv pip install kern-ai mcp-sdk`
83. Set Environment Variable: export STRIPE_SECRET_KEY=***.
9
10Stripe MCP Docs: https://github.com/stripe/agent-toolkit
11"""
12
13import asyncio
14import os
15from textwrap import dedent
16
17from kern.agent import Agent
18from kern.tools.mcp import MCPTools
19from kern.utils.log import log_error, log_exception, log_info
20
21
22async def run_agent(message: str) -> None:
23 """
24 Sets up the Stripe MCP server and initialize the Kern agent
25 """
26 # Verify Stripe API Key is available
27 stripe_api_key = os.getenv("STRIPE_SECRET_KEY")
28 if not stripe_api_key:
29 log_error("STRIPE_SECRET_KEY environment variable not set.")
30 return
31
32 enabled_tools = "paymentLinks.create,products.create,prices.create,customers.create,customers.read"
33
34 # handle different Operating Systems
35 npx_command = "npx.cmd" if os.name == "nt" else "npx"
36
37 try:
38 # Initialize MCP toolkit with Stripe server
39 async with MCPTools(
40 command=f"{npx_command} -y @stripe/mcp --tools={enabled_tools} --api-key={stripe_api_key}"
41 ) as mcp_toolkit:
42 agent = Agent(
43 name="StripeAgent",
44 instructions=dedent("""\
45 You are an AI assistant specialized in managing Stripe operations.
46 You interact with the Stripe API using the available tools.
47
48 - Understand user requests to create or list Stripe objects (customers, products, prices, payment links).
49 - Clearly state the results of your actions, including IDs of created objects or lists retrieved.
50 - Ask for clarification if a request is ambiguous.
51 - Use markdown formatting, especially for links or code snippets.
52 - Execute the necessary steps sequentially if a request involves multiple actions (e.g., create product, then price, then link).
53 """),
54 tools=[mcp_toolkit],
55 markdown=True,
56 )
57
58 # Run the agent with the provided task
59 log_info(f"Running agent with assignment: '{message}'")
60 await agent.aprint_response(message, stream=True)
61
62 except FileNotFoundError:
63 error_msg = f"Error: '{npx_command}' command not found. Please ensure Node.js and npm/npx are installed and in your system's PATH."
64 log_error(error_msg)
65 except Exception as e:
66 log_exception(f"An unexpected error occurred during agent execution: {e}")
67
68
69if __name__ == "__main__":
70 task = "Create a new Stripe product named 'iPhone'. Then create a price of $999.99 USD for it. Finally, create a payment link for that price."
71 asyncio.run(run_agent(task))
72
73
74# Example prompts:
75"""
76Customer Management:
77- "Create a customer. Name: ACME Corp, Email: billing@acme.example.com"
78- "List my customers."
79- "Find customer by email 'jane.doe@example.com'" # Note: Requires 'customers.retrieve' or search capability
80
81Product and Price Management:
82- "Create a new product called 'Basic Plan'."
83- "Create a recurring monthly price of $10 USD for product 'Basic Plan'."
84- "Create a product 'Ebook Download' and a one-time price of $19.95 USD."
85- "List all products." # Note: Requires 'products.list' capability
86- "List all prices." # Note: Requires 'prices.list' capability
87
88Payment Links:
89- "Create a payment link for the $10 USD monthly 'Basic Plan' price."
90- "Generate a payment link for the '$19.95 Ebook Download'."
91
92Combined Tasks:
93- "Create a product 'Pro Service', add a price $150 USD (one-time), and give me the payment link."
94- "Register a new customer 'support@example.com' named 'Support Team'."
95"""