You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Pact MCP Server (SmartBear)
  2. ## Principle
  3. Use the SmartBear MCP server to enable AI agent interaction with PactFlow/Pact Broker during contract testing workflows. The MCP server provides tools for generating pact tests, fetching provider states, reviewing test quality, and checking deployment safety — all accessible through the Model Context Protocol.
  4. ## Rationale
  5. ### Why MCP for contract testing?
  6. - **Live broker queries**: AI agents can fetch existing provider states, verification results, and deployment status directly from PactFlow
  7. - **Test generation assistance**: MCP tools generate consumer and provider tests based on existing contracts, OpenAPI specs, or templates
  8. - **Automated review**: MCP-powered review checks tests against best practices without manual inspection
  9. - **Deployment safety**: `can-i-deploy` checks integrated into agent workflows for real-time compatibility verification
  10. ### When TEA uses it
  11. - **test-design workflow**: Fetch existing provider states to understand current contract landscape
  12. - **automate workflow**: Generate pact tests using broker knowledge and existing contracts
  13. - **test-review workflow**: Review pact tests against best practices with automated feedback
  14. - **ci workflow**: Reference can-i-deploy and matrix tools for pipeline guidance
  15. ## Available Tools
  16. | # | Tool | Description | When Used |
  17. | --- | ------------------------- | ----------------------------------------------------------------------- | --------------------- |
  18. | 1 | **Generate Pact Tests** | Create consumer/provider tests from code, OpenAPI, or templates | automate workflow |
  19. | 2 | **Fetch Provider States** | List all provider states from broker for a given consumer-provider pair | test-design, automate |
  20. | 3 | **Review Pact Tests** | Analyze tests against contract testing best practices | test-review |
  21. | 4 | **Can I Deploy** | Check deployment safety via broker verification matrix | ci workflow |
  22. | 5 | **Matrix** | Query consumer-provider verification matrix | ci, test-design |
  23. | 6 | **PactFlow AI Status** | Check AI credits and permissions (PactFlow Cloud only) | diagnostics |
  24. | 7 | **Metrics - All** | Workspace-wide contract testing metrics | reporting |
  25. | 8 | **Metrics - Team** | Team-level adoption statistics (PactFlow Cloud only) | reporting |
  26. ## Installation
  27. ### Config file locations
  28. | Tool | Global Config File | Format |
  29. | ----------------- | ------------------------------------- | ---------------------- |
  30. | Claude Code | `~/.claude.json` | JSON (`mcpServers`) |
  31. | Codex | `~/.codex/config.toml` | TOML (`[mcp_servers]`) |
  32. | Gemini CLI | `~/.gemini/settings.json` | JSON (`mcpServers`) |
  33. | Cursor | `~/.cursor/mcp.json` | JSON (`mcpServers`) |
  34. | Windsurf | `~/.codeium/windsurf/mcp_config.json` | JSON (`mcpServers`) |
  35. | VS Code (Copilot) | `.vscode/mcp.json` | JSON (`servers`) |
  36. > **Claude Code tip**: Prefer the `claude mcp add` CLI over manual JSON editing. Use `-s user` for global (all projects) or omit for per-project (default).
  37. ### CLI shortcuts (Claude Code and Codex)
  38. ```bash
  39. # Claude Code — use add-json for servers with env vars (-s user = global)
  40. claude mcp add-json -s user smartbear \
  41. '{"type":"stdio","command":"npx","args":["-y","@smartbear/mcp@latest"],"env":{"PACT_BROKER_BASE_URL":"https://{tenant}.pactflow.io","PACT_BROKER_TOKEN":"<your-token>"}}'
  42. # Codex
  43. codex mcp add smartbear -- npx -y @smartbear/mcp@latest
  44. ```
  45. ### JSON config (Gemini CLI, Cursor, Windsurf)
  46. Add a `"smartbear"` entry to the `mcpServers` object in the config file for your tool:
  47. ```json
  48. {
  49. "mcpServers": {
  50. "smartbear": {
  51. "type": "stdio",
  52. "command": "npx",
  53. "args": ["-y", "@smartbear/mcp@latest"],
  54. "env": {
  55. "PACT_BROKER_BASE_URL": "https://{tenant}.pactflow.io",
  56. "PACT_BROKER_TOKEN": "<your-api-token>"
  57. }
  58. }
  59. }
  60. }
  61. ```
  62. ### Codex TOML config
  63. Codex uses TOML instead of JSON. Add to `~/.codex/config.toml`:
  64. ```toml
  65. [mcp_servers.smartbear]
  66. command = "npx"
  67. args = ["-y", "@smartbear/mcp@latest"]
  68. [mcp_servers.smartbear.env]
  69. PACT_BROKER_BASE_URL = "https://{tenant}.pactflow.io"
  70. PACT_BROKER_TOKEN = "<your-api-token>"
  71. ```
  72. Note the key is `mcp_servers` (underscored), not `mcpServers`.
  73. ### VS Code (GitHub Copilot)
  74. Add to `.vscode/mcp.json` (note: uses `servers` key, not `mcpServers`):
  75. ```json
  76. {
  77. "servers": {
  78. "smartbear": {
  79. "type": "stdio",
  80. "command": "npx",
  81. "args": ["-y", "@smartbear/mcp@latest"],
  82. "env": {
  83. "PACT_BROKER_BASE_URL": "https://{tenant}.pactflow.io",
  84. "PACT_BROKER_TOKEN": "${input:pactToken}"
  85. }
  86. }
  87. }
  88. }
  89. ```
  90. > **Note**: Set either `PACT_BROKER_TOKEN` (for PactFlow) or `PACT_BROKER_USERNAME`+`PACT_BROKER_PASSWORD` (for self-hosted). Leave unused vars empty.
  91. ## Required Environment Variables
  92. | Variable | Required | Description |
  93. | ---------------------- | ---------------------------- | --------------------------------------- |
  94. | `PACT_BROKER_BASE_URL` | Yes (for Pact features) | PactFlow or self-hosted Pact Broker URL |
  95. | `PACT_BROKER_TOKEN` | For PactFlow / token auth | API token for broker authentication |
  96. | `PACT_BROKER_USERNAME` | For basic auth (self-hosted) | Username for basic authentication |
  97. | `PACT_BROKER_PASSWORD` | For basic auth (self-hosted) | Password for basic authentication |
  98. **Authentication**: Use token auth (`PACT_BROKER_TOKEN`) for PactFlow. Use basic auth (`PACT_BROKER_USERNAME` + `PACT_BROKER_PASSWORD`) for self-hosted Pact Broker instances. Only one auth method is needed.
  99. **Requirements**: Node.js 20+
  100. ## Pattern Examples
  101. ### Example 1: Fetching Provider States During Test Design
  102. When designing contract tests, use MCP to query existing provider states:
  103. ```
  104. # Agent queries SmartBear MCP during test-design workflow:
  105. # → Fetch Provider States for consumer="movie-web", provider="SampleMoviesAPI"
  106. # ← Returns: ["movie with id 1 exists", "no movies exist", "user is authenticated"]
  107. #
  108. # Agent uses this to generate comprehensive consumer tests covering all states
  109. ```
  110. ### Example 2: Reviewing Pact Tests
  111. During test-review workflow, use MCP to evaluate test quality:
  112. ```
  113. # Agent submits test file to SmartBear MCP Review tool:
  114. # → Review Pact Tests with test file content
  115. # ← Returns: feedback on matcher usage, state coverage, interaction naming
  116. #
  117. # Agent incorporates feedback into review report
  118. ```
  119. ### Example 3: Can I Deploy Check in CI
  120. During CI workflow design, reference the can-i-deploy tool:
  121. ```
  122. # Agent generates CI pipeline with can-i-deploy gate:
  123. # → Can I Deploy: pacticipant="SampleMoviesAPI", version="${GITHUB_SHA}", to="production"
  124. # ← Returns: { ok: true/false, reason: "..." }
  125. #
  126. # Agent designs pipeline to block deployment if can-i-deploy fails
  127. ```
  128. ## Key Points
  129. - **Per-project install recommended**: Different projects may target different PactFlow tenants — match TEA's per-project config philosophy
  130. - **Env vars are project-specific**: `PACT_BROKER_BASE_URL` and `PACT_BROKER_TOKEN` vary by project/team
  131. - **Node.js 20+ required**: SmartBear MCP server requires Node.js 20 or higher
  132. - **PactFlow Cloud features**: Some tools (AI Status, Team Metrics) are only available with PactFlow Cloud, not self-hosted Pact Broker
  133. - **Complements pactjs-utils**: MCP provides broker interaction during design/review; pactjs-utils provides runtime utilities for test code
  134. ## Related Fragments
  135. - `pactjs-utils-overview.md` — runtime utilities that pact tests import
  136. - `pactjs-utils-provider-verifier.md` — verifier options that reference broker config
  137. - `pact-broker-webhooks.md` — PactFlow → GitHub webhook auth pattern and staleness monitoring; `Metrics - All` / `Matrix` MCP tools are useful here for dashboards
  138. - `contract-testing.md` — foundational contract testing patterns
  139. ## Anti-Patterns
  140. ### Wrong: Using MCP for runtime test execution
  141. ```
  142. # ❌ Don't use MCP to run pact tests — use npm scripts and CI pipelines
  143. # MCP is for agent-assisted design, generation, and review
  144. ```
  145. ### Right: Use MCP for design-time assistance
  146. ```
  147. # ✅ Use MCP during planning and review:
  148. # - Fetch provider states to inform test design
  149. # - Generate test scaffolds from existing contracts
  150. # - Review tests for best practice compliance
  151. # - Check can-i-deploy during CI pipeline design
  152. ```
  153. _Source: SmartBear MCP documentation, PactFlow developer docs_