|
1 | | -# PostHog MCP Server - Python Implementation |
| 1 | +# posthog-agent-toolkit |
2 | 2 |
|
3 | | -Python wrapper of the MCP server tools to make using PostHog easier in agents. |
| 3 | +Tools to give agents access to your PostHog data, manage feature flags, create insights, and more. |
4 | 4 |
|
5 | | -## Setup |
| 5 | +This is a Python wrapper around the PostHog MCP (Model Context Protocol) server, providing easy integration with AI frameworks like LangChain. |
6 | 6 |
|
7 | | -### 1. Install Dependencies with uv |
8 | | - |
9 | | -For production: |
10 | | - |
11 | | -```bash |
12 | | -uv sync |
13 | | -``` |
14 | | - |
15 | | -For development (includes schema generation tools): |
| 7 | +## Installation |
16 | 8 |
|
17 | 9 | ```bash |
18 | | -uv sync --dev |
| 10 | +pip install posthog-agent-toolkit |
19 | 11 | ``` |
20 | 12 |
|
21 | | -### 2. Alternative: Create Virtual Environment Manually |
| 13 | +## Quick Start |
22 | 14 |
|
23 | | -If you prefer to use pip: |
| 15 | +The toolkit provides integrations for popular AI frameworks: |
24 | 16 |
|
25 | | -```bash |
26 | | -python3 -m venv venv |
27 | | -source venv/bin/activate # On Windows: venv\Scripts\activate |
28 | | -pip install -e . |
29 | | -``` |
| 17 | +### Using with LangChain |
30 | 18 |
|
31 | | -## Development |
| 19 | +```python |
| 20 | +from langchain_openai import ChatOpenAI |
| 21 | +from langchain.agents import AgentExecutor, create_tool_calling_agent |
| 22 | +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder |
| 23 | +from posthog_agent_toolkit.integrations.langchain.toolkit import PostHogAgentToolkit |
32 | 24 |
|
33 | | -### Schema Generation |
| 25 | +# Initialize the PostHog toolkit |
| 26 | +toolkit = PostHogAgentToolkit( |
| 27 | + personal_api_key="your_posthog_personal_api_key", |
| 28 | + url="https://mcp.posthog.com/mcp" # or your own, if you are self hosting the MCP server |
| 29 | +) |
34 | 30 |
|
35 | | -The Python implementation uses Pydantic models generated from the TypeScript Zod schemas: |
| 31 | +# Get the tools |
| 32 | +tools = await toolkit.get_tools() |
36 | 33 |
|
37 | | -```bash |
38 | | -# From the root directory |
39 | | -pnpm run schema:build # Generate both JSON schema and Python models |
40 | | -``` |
| 34 | +# Initialize the LLM |
| 35 | +llm = ChatOpenAI(model="gpt-5-mini") |
41 | 36 |
|
42 | | -This generates type-safe Pydantic models in `schema/tool_inputs.py` based on the shared JSON schema. |
| 37 | +# Create a prompt |
| 38 | +prompt = ChatPromptTemplate.from_messages([ |
| 39 | + ("system", "You are a data analyst with access to PostHog analytics"), |
| 40 | + ("human", "{input}"), |
| 41 | + MessagesPlaceholder("agent_scratchpad"), |
| 42 | +]) |
43 | 43 |
|
44 | | -### Project Structure |
| 44 | +# Create and run the agent |
| 45 | +agent = create_tool_calling_agent(llm=llm, tools=tools, prompt=prompt) |
| 46 | +executor = AgentExecutor(agent=agent, tools=tools) |
45 | 47 |
|
| 48 | +result = await executor.ainvoke({ |
| 49 | + "input": "Analyze our product usage by getting the top 5 most interesting insights and summarising the data from them." |
| 50 | +}) |
46 | 51 | ``` |
47 | | -python/ |
48 | | -βββ pyproject.toml # Project configuration and dependencies |
49 | | -βββ uv.lock # Locked dependency versions (generated by uv) |
50 | | -βββ schema/ |
51 | | -β βββ tool_inputs.py # Generated Pydantic models |
52 | | -βββ scripts/ |
53 | | - βββ generate-pydantic-models.sh # Schema generation script |
54 | | -``` |
55 | | - |
56 | | -### Running Commands |
57 | | - |
58 | | -All Python commands should be run through uv: |
59 | 52 |
|
60 | | -```bash |
61 | | -# Run tests |
62 | | -uv run pytest |
63 | | - |
64 | | -# Format code |
65 | | -uv run ruff format . |
66 | | - |
67 | | -# Lint code |
68 | | -uv run ruff check --fix . |
69 | | -``` |
| 53 | +**[β See full LangChain example](https://github.com/posthog/mcp/tree/main/examples/langchain)** |
70 | 54 |
|
71 | | -### Dependencies |
| 55 | +## Available Tools |
72 | 56 |
|
73 | | -Dependencies are managed in `pyproject.toml`. |
| 57 | +For a list of all available tools, please see the [docs](https://posthog.com/docs/model-context-protocol). |
0 commit comments