Pipedream Auth

This example shows how to add authorization when integrating Pipedream MCP servers with Kern Agents.

Code

1"""
2 Using Pipedream MCP servers with authentication
3
4This is an example of how to use Pipedream MCP servers with authentication.
5This is useful if your app is interfacing with the MCP servers in behalf of your users.
6
71. Get your access token. You can check how in Pipedream's docs: https://pipedream.com/docs/connect/mcp/developers/
82. Get the URL of the MCP server. It will look like this: https://remote.mcp.pipedream.net/<External user id>/<MCP app slug>
93. Set the environment variables:
10 - MCP_SERVER_URL: The URL of the MCP server you previously got
11 - MCP_ACCESS_TOKEN: The access token you previously got
12 - PIPEDREAM_PROJECT_ID: The project id of the Pipedream project you want to use
13 - PIPEDREAM_ENVIRONMENT: The environment of the Pipedream project you want to use
143. Install dependencies: uv pip install kern-ai mcp-sdk
15"""
16
17import asyncio
18from os import getenv
19
20from kern.agent import Agent
21from kern.models.openai import OpenAIResponses
22from kern.tools.mcp import MCPTools, StreamableHTTPClientParams
23from kern.utils.log import log_exception
24
25mcp_server_url = getenv("MCP_SERVER_URL")
26mcp_access_token = getenv("MCP_ACCESS_TOKEN")
27pipedream_project_id = getenv("PIPEDREAM_PROJECT_ID")
28pipedream_environment = getenv("PIPEDREAM_ENVIRONMENT")
29
30
31server_params = StreamableHTTPClientParams(
32 url=mcp_server_url,
33 headers={
34 "Authorization": f"Bearer {mcp_access_token}",
35 "x-pd-project-id": pipedream_project_id,
36 "x-pd-environment": pipedream_environment,
37 },
38)
39
40
41async def run_agent(task: str) -> None:
42 try:
43 async with MCPTools(
44 server_params=server_params, transport="streamable-http", timeout_seconds=20
45 ) as mcp:
46 agent = Agent(
47 model=OpenAIResponses(id="gpt-5.2"),
48 tools=[mcp],
49 markdown=True,
50 )
51 await agent.aprint_response(message=task, stream=True)
52 except Exception as e:
53 log_exception(f"Unexpected error: {e}")
54
55
56if __name__ == "__main__":
57 # The agent can read channels, users, messages, etc.
58 asyncio.run(run_agent("Show me the latest message in the channel #general"))