Symfony MCP教程-客户管理演示
学习如何使用Symfony构建模型上下文协议(MCP)服务器!此存储库包含一个完整的Symfony应用程序,其中包含英国客户数据,可供您添加MCP服务器功能。
教程结构
starter分支 -不带MCP的完整Symfony应用程序(从这里开始!)main分支 -完整的教程文档
快速开始
# Clone the repository
git clone git@github.com:GregHolmes/build-a-symfony-mcp-server-tutorial.git
cd app
# Start with the starter branch
git checkout starter
# Install dependencies
composer install
# View the web interface
php -S localhost:8000 -t public包含什么
此Symfony应用程序包括:
- 客户管理:100名假客户,提供真实数据
- 产品目录:6个类别的50种产品
- 订单历史:每位客户有1-5个订单,包含多个商品
- SQLite数据库:轻量级、可移植的数据库(无需设置)
- ORM原则:完整的实体关系和存储库
数据库模式
实体
- 客户:名字、姓氏、电子邮件、电话、地址、城市、州、邮政编码、创建地址
- 产品:名称、描述、价格、类别
- 订单:客户、订单日期、状态、总计
- 订单项:订单、产品、数量、价格
关系
- 客户有多个订单(一对多)
- 订单有多个OrderItem(一对多)
- OrderItem属于产品(多对一)
安装
应用程序已设置!该数据库已创建并填充了虚假数据。
Web管理界面
包含一个简单的管理界面,用于浏览客户、订单和产品。
启动Web服务器
php -S localhost:8000 -t public然后打开浏览器:
- http://localhost:8000 -主页(客户列表)
- http://localhost:8000/customers -所有客户
- http://localhost:8000/customers/1 -客户详细信息页面
- http://localhost:8000/orders -所有订单
- http://localhost:8000/products -所有产品
管理界面显示:
- 客户名单,包括订单数量和总支出
- 包含订单历史的客户详细信息页面
- 带有状态徽章的订单列表
- 产品目录,包括类别和定价
重新生成数据(可选)
如果你想重新生成假数据:
php bin/console doctrine:fixtures:load这将:
- 清空数据库
- 创建50个产品
- 创建100个客户
- 为每位客户创建1-5个订单
- 为每个订单添加1-5个项目
查询数据库
使用Symfony控制台
# Count customers
php bin/console dbal:run-sql "SELECT COUNT(*) FROM customers"
# List customers
php bin/console dbal:run-sql "SELECT * FROM customers LIMIT 10"
# Find customers by state
php bin/console dbal:run-sql "SELECT * FROM customers WHERE state = 'CA'"
# Get customer with orders
php bin/console dbal:run-sql "SELECT c.first_name, c.last_name, COUNT(o.id) as order_count FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.id LIMIT 10"使用PHP/原则
在中创建自定义命令 src/Command/ 查询客户:
use App\Repository\CustomerRepository;
$customers = $customerRepository->findAll();
$searchResults = $customerRepository->findBySearchTerm('john');
$topSpenders = $customerRepository->findTopSpenders(10);数据库位置
SQLite数据库位于:
var/data.db您可以使用任何SQLite客户端打开它,也可以使用Symfony控制台命令。
MCP教程用例
此应用程序非常适合演示MCP,因为您可以构建工具来:
- 搜索客户:按姓名、电子邮件、城市或州查找客户
- 获取客户详细信息:检索包含订单历史的完整客户资料
- 分析订单:获取最近的订单、订单统计、收入数据
- 客户洞察:按地点、订单模式划分的顶级消费者、客户
MCP集成的下一步
当您准备添加MCP时:
- 创建MCP服务器(PHP或Node.js)
- 连接到SQLite数据库
var/data.db - 展示以下工具:
- search_customers - get_customer_details - get_customer_orders - get_order_stats
- 使用Claude Desktop或其他MCP客户端进行测试
技术栈
- PHP: 8.4.13
- 交响乐: 7.2
- ORM原则:最新
- SQLite:3.x
- 伪造者:用于真实的假数据
项目结构
app/
├── src/
│ ├── Entity/ # Customer, Order, Product, OrderItem
│ ├── Repository/ # Database queries
│ └── DataFixtures/ # Fake data generation
├── var/
│ └── data.db # SQLite database
└── config/
└── packages/
└── doctrine.yaml # Database configuration