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 Operations23This 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.456Setup:72. Install Python dependencies: `uv pip install kern-ai mcp-sdk`83. Set Environment Variable: export STRIPE_SECRET_KEY=***.910Stripe MCP Docs: https://github.com/stripe/agent-toolkit11"""1213import asyncio14import os15from textwrap import dedent1617from kern.agent import Agent18from kern.tools.mcp import MCPTools19from kern.utils.log import log_error, log_exception, log_info202122async def run_agent(message: str) -> None:23 """24 Sets up the Stripe MCP server and initialize the Kern agent25 """26 # Verify Stripe API Key is available27 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 return3132 enabled_tools = "paymentLinks.create,products.create,prices.create,customers.create,customers.read"3334 # handle different Operating Systems35 npx_command = "npx.cmd" if os.name == "nt" else "npx"3637 try:38 # Initialize MCP toolkit with Stripe server39 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.4748 - 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 )5758 # Run the agent with the provided task59 log_info(f"Running agent with assignment: '{message}'")60 await agent.aprint_response(message, stream=True)6162 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}")676869if __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))727374# 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 capability8081Product 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' capability86- "List all prices." # Note: Requires 'prices.list' capability8788Payment 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'."9192Combined 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"""