🚀 Planning Center Python
这是一个全面且现代化的Python包装器,用于与Planning Center API进行交互。它借助Pydantic和异步/等待模式,为所有Planning Center产品(包括人员管理、服务安排、签到系统、捐赠管理、群组活动和日程安排等)提供类型安全的访问方式。
🚀 快速开始
基础用法
import asyncio
from planning_center_api import PCOClient, PCOProduct
async def main():
async with PCOClient(app_id="your_app_id", secret="your_secret") as client:
people = await client.get_people(include=["emails"])
person = await client.create_person({
"first_name": "John",
"last_name": "Doe"
})
async for service in client.paginate_all(
product=PCOProduct.SERVICES,
resource="services"
):
print(f"Service: {service.attributes.get('title')}")
asyncio.run(main())
身份验证
该库支持OAuth 2.0和个人访问令牌两种身份验证方式:
client = PCOClient(access_token="your_oauth_token")
client = PCOClient(app_id="your_app_id", secret="your_secret")
处理Webhook
from fastapi import FastAPI, Request
from planning_center_api import PCOClient, handle_webhook_event
app = FastAPI()
client = PCOClient(webhook_secret="your_secret")
@app.post("/webhook")
async def webhook_handler(request: Request):
payload = await request.body()
signature = request.headers.get("x-pco-signature")
async def person_created(webhook_payload):
print(f"New person: {webhook_payload.resource.attributes}")
await handle_webhook_event(
client=client,
payload=payload.decode(),
signature=signature,
event_handlers={"people.created": person_created}
)
return {"status": "success"}
✨ 主要特性
- 类型安全:为所有数据结构提供完整的Pydantic模型。
- 异步/等待支持:使用httpx库,支持现代Python编程模式。
- 原生Webhook:内置签名验证和事件处理功能。
- 全产品支持:涵盖人员管理、服务安排、签到系统、捐赠管理、群组活动和日程安排等所有Planning Center产品。
- 自动分页:无缝处理大型数据集。
- 速率限制:具备自动指数退避机制。
- 全面的错误处理:提供特定的异常类型。
- CLI工具:用于常见操作。
- 实用函数:用于数据处理和分析。
- MCP服务器:使用FastAPI - MCP实现AI助手集成。
📦 安装指南
核心API包
cd planning-center-api
uv pip install "planning_center_api@."
cd planning-center-api
pip install .
MCP服务器(可选)
用于AI助手集成:
cd planning-center-mcp-server
uv pip install "planning_center_mcp@."
cd planning-center-mcp-server
pip install .
💻 使用示例
基础用法
import asyncio
from planning_center_api import PCOClient, PCOProduct
async def main():
async with PCOClient(app_id="your_app_id", secret="your_secret") as client:
people = await client.get_people(include=["emails"])
person = await client.create_person({
"first_name": "John",
"last_name": "Doe"
})
async for service in client.paginate_all(
product=PCOProduct.SERVICES,
resource="services"
):
print(f"Service: {service.attributes.get('title')}")
asyncio.run(main())
高级用法
import asyncio
from planning_center_api import PCOClient, PCOProduct
async def advanced_usage():
async with PCOClient(app_id="your_app_id", secret="your_secret") as client:
people = await client.get_people(include=["emails"])
active_people = [p for p in people if p.attributes.get('active')]
for person in active_people:
print(f"Active person: {person.attributes.get('first_name')} {person.attributes.get('last_name')}")
asyncio.run(advanced_usage())
📚 详细文档
核心客户端
PCOClient
用于Planning Center API操作的主要客户端。
async with PCOClient(
app_id="your_app_id",
secret="your_secret",
access_token="your_token",
webhook_secret="your_webhook_secret"
) as client:
通用CRUD操作
people = await client.get(PCOProduct.PEOPLE, "people")
person = await client.get(PCOProduct.PEOPLE, "people", "person_id")
new_person = await client.create(PCOProduct.PEOPLE, "people", {
"first_name": "John",
"last_name": "Doe"
})
updated_person = await client.update(
PCOProduct.PEOPLE, "people", "person_id", {
"phone": "555-123-4567"
}
)
success = await client.delete(PCOProduct.PEOPLE, "people", "person_id")
分页操作
async for person in client.paginate_all(
product=PCOProduct.PEOPLE,
resource="people",
per_page=25
):
print(person.get_full_name())
特定产品方法
人员管理
people = await client.get_people(per_page=50, include=["emails", "phone_numbers"])
person = await client.get_person("person_id", include=["emails"])
results = await client.search_people("john")
email_results = await client.get_people_by_email("john@example.com")
active_people = await client.get_active_people()
person = await client.create_person({
"first_name": "John",
"last_name": "Doe",
"email": "john@example.com"
})
updated = await client.update_person("person_id", {"phone": "555-123-4567"})
服务安排
services = await client.get_services(per_page=25)
service = await client.get_service("service_id")
plans = await client.get_plans(service_id="service_id")
plan = await client.get_plan("plan_id")
🖥 CLI使用方法
该库包含一个全面的CLI工具:
pco-cli get --product people --resource people --per-page 10
pco-cli search-people --query "john" --output table
pco-cli create --product people --resource people --data '{"first_name": "John", "last_name": "Doe"}'
pco-cli paginate --product services --resource services --output csv
pco-cli find-by-email --email "john@example.com"
🤖 用于AI助手的MCP服务器
该仓库包含一个全面的模型上下文协议(MCP)服务器,它将所有主要的Planning Center API作为工具暴露给像Claude、Cursor等MCP兼容的AI助手客户端。
特性
- 全面覆盖:涵盖9个Planning Center产品的65 + 只读工具。
- 原生MCP集成:使用官方MCP协议构建,实现与AI助手的无缝集成。
- 只读操作:确保数据安全。
- 包含模拟服务器:无需API凭证即可进行测试。
- 支持Claude桌面版:预配置设置。
- 高级过滤和分页支持。
- 活动参与者过滤:增强活动管理功能。
可用的MCP工具(65 + 工具)
人员API(15个工具)
get_people
、get_person
、get_person_addresses
、get_person_emails
、get_person_phones
、get_person_background_checks
、get_field_definitions
、get_forms
、get_form
、get_campuses
、get_campus
服务API(12个工具)
get_services
、get_service
、get_plans
、get_plan
、get_songs
、get_song
、get_arrangements
、get_arrangement
、get_keys
、get_key
、get_teams
、get_team
、get_team_positions
、get_team_position
注册API(5个工具)
get_registrations
、get_registration
、get_attendees
、get_attendee
、get_event_attendees
捐赠API(10个工具)
get_donations
、get_donation
、get_funds
、get_fund
、get_batches
、get_batch
、get_pledges
、get_pledge
、get_pledge_campaigns
、get_pledge_campaign
、get_recurring_donations
、get_recurring_donation
群组API(8个工具)
get_groups
、get_group
、get_group_events
、get_group_event
、get_group_memberships
、get_group_membership
、get_group_types
、get_group_type
日历API(2个工具)
get_calendar_events
、get_calendar_event
签到API(4个工具)
get_check_in_events
、get_check_in_event
、get_locations
、get_location
发布API(4个工具)
get_channels
、get_channel
、get_episodes
、get_episode
Webhook API(2个工具)
get_webhook_subscriptions
、get_webhook_subscription
组织API(4个工具)
get_connected_applications
、get_connected_application
、get_oauth_applications
、get_oauth_application
快速开始
选项1:模拟服务器(无需凭证)
适用于测试和开发:
cd planning-center-mcp-server
uv run python mcp_mock_server_comprehensive.py
run_comprehensive_server.bat
选项2:真实服务器(需要API凭证)
用于生产环境,处理真实的Planning Center数据:
cp env.example .env
uv run python mcp_server_fixed.py
run_real_server.bat
与Claude桌面版集成
服务器已预配置,可与Claude桌面版集成:
- 编辑Claude桌面版配置文件:
%APPDATA%\Claude\claude_desktop_config.json
- 添加服务器配置(如果按照设置步骤操作,此步骤已完成)
- 重启Claude桌面版
- 开始询问关于Planning Center数据的问题
Claude桌面版可用选项
- 模拟服务器:
planning-center-mock
(无需凭证)
- 真实服务器:
planning-center-api
(需要API凭证)
使用示例
配置完成后,你可以向Claude询问以下问题:
- "显示我们数据库中所有活跃人员"
- "获取2024年夏令营的参与者"
- "查找上个月的所有捐赠记录"
- "列出所有群组及其成员"
- "显示即将到来的日历事件"
模拟服务器特性
- 14 + 工具:涵盖最常用的端点。
- 逼真的模拟数据:实体之间具有正确的关系。
- 无需身份验证:便于测试。
- 与真实服务器相同的API结构。
- 支持MCP集成:用于AI助手测试。
详细的MCP服务器文档,请参阅 planning-center-mcp-server/README.md。
CLI配置
可以设置环境变量或使用配置文件:
export PCO_APP_ID="your_app_id"
export PCO_SECRET="your_secret"
export PCO_ACCESS_TOKEN="your_token"
或者创建一个config.json
文件:
{
"app_id": "your_app_id",
"secret": "your_secret",
"timeout": 30.0,
"max_retries": 3
}
🚨 错误处理
该库为不同的错误场景提供特定的异常类型:
from planning_center_api.exceptions import (
PCOError,
PCOAuthenticationError,
PCOPermissionError,
PCONotFoundError,
PCOValidationError,
PCORateLimitError,
PCOServerError
)
try:
person = await client.get_person("invalid_id")
except PCONotFoundError:
print("人员未找到")
except PCOAuthenticationError:
print("身份验证失败")
except PCORateLimitError as e:
print(f"速率受限。请在 {e.retry_after} 后重试")
except PCOError as e:
print(f"API错误: {e.message}")
📊 数据模型
所有数据都以Pydantic模型的形式返回,确保类型安全:
person = await client.get_person("person_id")
print(person.get_first_name())
print(person.get_email())
print(person.get_full_name())
people = await client.get_people()
print(len(people))
for person in people:
print(person.get_full_name())
person = await client.get_person("person_id", include=["emails"])
emails = person.get_relationship_data("emails")
🔄 速率限制
该库通过指数退避机制自动处理速率限制:
config = PCOConfig(
rate_limit_requests=100,
rate_limit_window=60,
max_retries=3,
backoff_factor=2.0
)
🧪 测试
cd planning-center-api
pytest
pytest --cov=planning_center_api
pytest tests/test_client.py
📝 示例
查看 planning-center-api/examples/
目录以获取全面的示例:
basic_usage.py
- 基本API操作
webhook_server.py
- FastAPI Webhook服务器
data_export.py
- 数据导出和分析
advanced_usage.py
- 高级模式和实用工具
🛠 开发
环境设置
git clone <repository-url>
cd planningcenter-wrapper
cd planning-center-api
uv sync --group dev
uv run ruff check --fix planning_center_api/
uv run pytest
项目结构
planningcenter-wrapper/
├── planning-center-api/ # 核心API包
│ ├── planning_center_api/ # 源代码
│ ├── examples/ # 使用示例
│ ├── tests/ # 测试套件
│ ├── docs/ # 文档
│ └── pyproject.toml # 包配置
├── planning-center-mcp-server/ # 用于AI助手的全面MCP服务器
│ ├── planning_center_mcp/ # MCP服务器源代码
│ ├── mcp_server_fixed.py # 包含65 + 工具的真实服务器
│ ├── mcp_mock_server_comprehensive.py # 用于测试的模拟服务器
│ ├── run_real_server.bat # 真实服务器的批处理文件
│ ├── run_comprehensive_server.bat # 模拟服务器的批处理文件
│ ├── README.md # 全面的MCP服务器文档
│ ├── COMPREHENSIVE_API_COVERAGE.md # 完整的API覆盖文档
│ ├── CLAUDE_DESKTOP_SETUP.md # Claude桌面版设置指南
│ ├── QUICKSTART.md # 快速开始指南
│ └── pyproject.toml # MCP服务器配置
├── _apis/ # API文档
└── README.md # 本文件
🤝 贡献
- 分叉仓库
- 创建功能分支
- 进行更改
- 添加测试
- 提交拉取请求
📄 许可证
本项目采用MIT许可证,详情请参阅 LICENSE 文件。
🆘 支持
- 查看 示例 目录。
- 查阅 API文档。
- MCP服务器文档:
- 全面的MCP服务器README
- 完整的API覆盖指南
- Claude桌面版设置指南
- 快速开始指南
- 如有Bug或功能请求,请提交问题。
🔗 链接