mcp-openapi-schema-explorer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/.devcontainer/devcontainer.json +24 -0
  2. package/.github/dependabot.yml +13 -0
  3. package/.github/workflows/ci.yml +111 -0
  4. package/.husky/pre-commit +6 -0
  5. package/.prettierignore +3 -0
  6. package/.prettierrc.json +12 -0
  7. package/.releaserc.json +23 -0
  8. package/CHANGELOG.md +32 -0
  9. package/CONTRIBUTING.md +67 -0
  10. package/Dockerfile +3 -0
  11. package/LICENSE +21 -0
  12. package/README.md +127 -0
  13. package/dist/src/config.d.ts +15 -0
  14. package/dist/src/config.js +19 -0
  15. package/dist/src/config.js.map +1 -0
  16. package/dist/src/handlers/component-detail-handler.d.ts +14 -0
  17. package/dist/src/handlers/component-detail-handler.js +87 -0
  18. package/dist/src/handlers/component-detail-handler.js.map +1 -0
  19. package/dist/src/handlers/component-map-handler.d.ts +14 -0
  20. package/dist/src/handlers/component-map-handler.js +63 -0
  21. package/dist/src/handlers/component-map-handler.js.map +1 -0
  22. package/dist/src/handlers/handler-utils.d.ts +69 -0
  23. package/dist/src/handlers/handler-utils.js +180 -0
  24. package/dist/src/handlers/handler-utils.js.map +1 -0
  25. package/dist/src/handlers/operation-handler.d.ts +14 -0
  26. package/dist/src/handlers/operation-handler.js +86 -0
  27. package/dist/src/handlers/operation-handler.js.map +1 -0
  28. package/dist/src/handlers/path-item-handler.d.ts +14 -0
  29. package/dist/src/handlers/path-item-handler.js +66 -0
  30. package/dist/src/handlers/path-item-handler.js.map +1 -0
  31. package/dist/src/handlers/top-level-field-handler.d.ts +14 -0
  32. package/dist/src/handlers/top-level-field-handler.js +72 -0
  33. package/dist/src/handlers/top-level-field-handler.js.map +1 -0
  34. package/dist/src/index.d.ts +2 -0
  35. package/dist/src/index.js +177 -0
  36. package/dist/src/index.js.map +1 -0
  37. package/dist/src/rendering/components.d.ts +67 -0
  38. package/dist/src/rendering/components.js +177 -0
  39. package/dist/src/rendering/components.js.map +1 -0
  40. package/dist/src/rendering/document.d.ts +36 -0
  41. package/dist/src/rendering/document.js +147 -0
  42. package/dist/src/rendering/document.js.map +1 -0
  43. package/dist/src/rendering/path-item.d.ts +45 -0
  44. package/dist/src/rendering/path-item.js +141 -0
  45. package/dist/src/rendering/path-item.js.map +1 -0
  46. package/dist/src/rendering/paths.d.ts +26 -0
  47. package/dist/src/rendering/paths.js +78 -0
  48. package/dist/src/rendering/paths.js.map +1 -0
  49. package/dist/src/rendering/types.d.ts +50 -0
  50. package/dist/src/rendering/types.js +12 -0
  51. package/dist/src/rendering/types.js.map +1 -0
  52. package/dist/src/rendering/utils.d.ts +31 -0
  53. package/dist/src/rendering/utils.js +79 -0
  54. package/dist/src/rendering/utils.js.map +1 -0
  55. package/dist/src/services/formatters.d.ts +36 -0
  56. package/dist/src/services/formatters.js +52 -0
  57. package/dist/src/services/formatters.js.map +1 -0
  58. package/dist/src/services/reference-transform.d.ts +27 -0
  59. package/dist/src/services/reference-transform.js +75 -0
  60. package/dist/src/services/reference-transform.js.map +1 -0
  61. package/dist/src/services/spec-loader.d.ts +27 -0
  62. package/dist/src/services/spec-loader.js +77 -0
  63. package/dist/src/services/spec-loader.js.map +1 -0
  64. package/dist/src/types.d.ts +11 -0
  65. package/dist/src/types.js +2 -0
  66. package/dist/src/types.js.map +1 -0
  67. package/dist/src/utils/uri-builder.d.ts +81 -0
  68. package/dist/src/utils/uri-builder.js +121 -0
  69. package/dist/src/utils/uri-builder.js.map +1 -0
  70. package/dist/src/version.d.ts +1 -0
  71. package/dist/src/version.js +4 -0
  72. package/dist/src/version.js.map +1 -0
  73. package/eslint.config.js +88 -0
  74. package/jest.config.js +32 -0
  75. package/justfile +66 -0
  76. package/memory-bank/activeContext.md +139 -0
  77. package/memory-bank/productContext.md +39 -0
  78. package/memory-bank/progress.md +141 -0
  79. package/memory-bank/projectbrief.md +50 -0
  80. package/memory-bank/systemPatterns.md +224 -0
  81. package/memory-bank/techContext.md +131 -0
  82. package/package.json +76 -0
  83. package/scripts/generate-version.js +49 -0
  84. package/src/config.ts +33 -0
  85. package/src/handlers/component-detail-handler.ts +121 -0
  86. package/src/handlers/component-map-handler.ts +92 -0
  87. package/src/handlers/handler-utils.ts +230 -0
  88. package/src/handlers/operation-handler.ts +114 -0
  89. package/src/handlers/path-item-handler.ts +88 -0
  90. package/src/handlers/top-level-field-handler.ts +92 -0
  91. package/src/index.ts +222 -0
  92. package/src/rendering/components.ts +228 -0
  93. package/src/rendering/document.ts +167 -0
  94. package/src/rendering/path-item.ts +157 -0
  95. package/src/rendering/paths.ts +87 -0
  96. package/src/rendering/types.ts +63 -0
  97. package/src/rendering/utils.ts +107 -0
  98. package/src/services/formatters.ts +71 -0
  99. package/src/services/reference-transform.ts +105 -0
  100. package/src/services/spec-loader.ts +88 -0
  101. package/src/types.ts +17 -0
  102. package/src/utils/uri-builder.ts +134 -0
  103. package/src/version.ts +4 -0
  104. package/test/__tests__/e2e/format.test.ts +224 -0
  105. package/test/__tests__/e2e/resources.test.ts +369 -0
  106. package/test/__tests__/e2e/spec-loading.test.ts +172 -0
  107. package/test/__tests__/unit/config.test.ts +39 -0
  108. package/test/__tests__/unit/handlers/component-detail-handler.test.ts +241 -0
  109. package/test/__tests__/unit/handlers/component-map-handler.test.ts +187 -0
  110. package/test/__tests__/unit/handlers/handler-utils.test.ts +255 -0
  111. package/test/__tests__/unit/handlers/operation-handler.test.ts +202 -0
  112. package/test/__tests__/unit/handlers/path-item-handler.test.ts +153 -0
  113. package/test/__tests__/unit/handlers/top-level-field-handler.test.ts +182 -0
  114. package/test/__tests__/unit/rendering/components.test.ts +269 -0
  115. package/test/__tests__/unit/rendering/document.test.ts +172 -0
  116. package/test/__tests__/unit/rendering/path-item.test.ts +197 -0
  117. package/test/__tests__/unit/rendering/paths.test.ts +115 -0
  118. package/test/__tests__/unit/services/formatters.test.ts +109 -0
  119. package/test/__tests__/unit/services/reference-transform.test.ts +320 -0
  120. package/test/__tests__/unit/services/spec-loader.test.ts +214 -0
  121. package/test/__tests__/unit/utils/uri-builder.test.ts +103 -0
  122. package/test/fixtures/complex-endpoint.json +146 -0
  123. package/test/fixtures/empty-api.json +8 -0
  124. package/test/fixtures/multi-component-types.json +55 -0
  125. package/test/fixtures/paths-test.json +61 -0
  126. package/test/fixtures/sample-api.json +68 -0
  127. package/test/fixtures/sample-v2-api.json +39 -0
  128. package/test/setup.ts +32 -0
  129. package/test/utils/console-helpers.ts +48 -0
  130. package/test/utils/mcp-test-helpers.ts +66 -0
  131. package/test/utils/test-types.ts +54 -0
  132. package/tsconfig.json +25 -0
  133. package/tsconfig.test.json +5 -0
@@ -0,0 +1,139 @@
1
+ # Active Context
2
+
3
+ ## Current Focus
4
+
5
+ Completed setup of automated versioning and releases using `semantic-release` and updated the CI workflow. Corrected dependency categorization in `package.json`. Updated Memory Bank.
6
+
7
+ ## Recent Progress
8
+
9
+ ### Dependency Cleanup & Release Automation (✓)
10
+
11
+ 1. **Dependency Correction:**
12
+ - Moved `swagger2openapi` from `devDependencies` to `dependencies` in `package.json`.
13
+ - Moved `@types/js-yaml` from `dependencies` to `devDependencies` in `package.json`.
14
+ - Removed unused `@types/swagger-parser` from `devDependencies` in `package.json`.
15
+ - Ran `npm install` to update `package-lock.json`.
16
+ 2. **Semantic Release Setup:**
17
+ - Installed `semantic-release` and plugins (`@semantic-release/commit-analyzer`, `@semantic-release/release-notes-generator`, `@semantic-release/changelog`, `@semantic-release/npm`, `@semantic-release/github`, `@semantic-release/git`, `@semantic-release/exec`) as dev dependencies.
18
+ - Created `scripts/generate-version.js` to write the release version to `src/version.ts`.
19
+ - Created `.releaserc.json` configuring the release workflow, including using `@semantic-release/exec` to run the generation script and `@semantic-release/git` to commit `src/version.ts`.
20
+ - Updated `eslint.config.js` to correctly lint the `generate-version.js` script.
21
+ 3. **Dynamic Versioning:**
22
+ - Created a default `src/version.ts` file (with version `0.0.0-dev`) tracked by Git to ensure local/CI builds work.
23
+ - Updated `src/index.ts` to import `VERSION` from `src/version.ts` and use it in the `McpServer` constructor.
24
+ - The default `src/version.ts` will be overwritten with the correct release version by `semantic-release` during the release process.
25
+ 4. **CI Workflow Adaptation:**
26
+ - Updated `.github/workflows/ci.yml`.
27
+ - Removed Docker Compose dependency from the `test` job.
28
+ - Standardized on Node 22 for `test` and `security` jobs.
29
+ - Added `extractions/setup-just@v3` action to both jobs.
30
+ - Updated `test` job to run checks via `just all`.
31
+ - Updated `security` job to run checks via `just security` (keeping CodeQL separate).
32
+ - Added a new `release` job triggered on pushes to `main` (after `test` and `security` pass) that runs `npx semantic-release`. Configured necessary permissions and environment variables (`GITHUB_TOKEN`, placeholder for `NPM_TOKEN`).
33
+ 5. **Memory Bank Update (✓):** Updated `activeContext.md`, `progress.md`, `systemPatterns.md`, and `techContext.md`.
34
+
35
+ ### Dynamic Server Name (✓)
36
+
37
+ 1. **Spec Loading:** Modified `src/index.ts` to load the OpenAPI spec using `createSpecLoader` _before_ initializing `McpServer`.
38
+ 2. **Name Generation:** Extracted `info.title` from the loaded spec and constructed a dynamic server name (`Schema Explorer for {title}`) with a fallback to `'OpenAPI Schema Explorer'`.
39
+ 3. **Server Initialization:** Updated `McpServer` constructor in `src/index.ts` to use the generated dynamic name.
40
+ 4. **Dependency Injection:** Confirmed handlers already receive the shared `specLoader` instance correctly, requiring no changes to handler constructors.
41
+ 5. **Memory Bank Update (✓):** Updated `activeContext.md` and `progress.md`.
42
+
43
+ ### Resource Completion Logic (✓)
44
+
45
+ 1. **Implementation:** Modified `src/index.ts` to define `ResourceTemplate` objects directly within `server.resource()` calls. Added `complete` property with functions providing suggestions for `{field}`, `{path}`, `{method*}`, and `{type}` based on the loaded `transformedSpec`.
46
+ 2. **Conditional Name Completion:** Implemented logic for the `{name*}` completion in the `openapi://components/{type}/{name*}` template. It now provides component names only if the spec contains exactly one component type (e.g., only `schemas`). Otherwise, it returns an empty list. Used `getValidatedComponentMap` helper for safe access.
47
+ 3. **Testing:**
48
+ - Added new E2E test suite (`Completion Tests`) to `test/__tests__/e2e/resources.test.ts` using the `client.complete()` method.
49
+ - Added new test fixture `test/fixtures/multi-component-types.json` to cover the multi-type scenario for name completion.
50
+ - Verified all tests pass.
51
+ 4. **Memory Bank Update (✓):** Updated `activeContext.md`, `progress.md`, `systemPatterns.md`, and `projectbrief.md`.
52
+
53
+ ### Dynamic Server Name (✓)
54
+
55
+ 1. **Spec Loading:** Modified `src/index.ts` to load the OpenAPI spec using `createSpecLoader` _before_ initializing `McpServer`.
56
+ 2. **Name Generation:** Extracted `info.title` from the loaded spec and constructed a dynamic server name (`Schema Explorer for {title}`) with a fallback to `'OpenAPI Schema Explorer'`.
57
+ 3. **Server Initialization:** Updated `McpServer` constructor in `src/index.ts` to use the generated dynamic name.
58
+ 4. **Dependency Injection:** Confirmed handlers already receive the shared `specLoader` instance correctly, requiring no changes to handler constructors.
59
+ 5. **Memory Bank Update (✓):** Updated `activeContext.md` and `progress.md`.
60
+
61
+ ### Minified JSON Output Format (✓)
62
+
63
+ 1. **Formatter:** Added `MinifiedJsonFormatter` to `src/services/formatters.ts` and updated `OutputFormat` type and `createFormatter` function.
64
+ 2. **Configuration:** Updated `src/config.ts` to accept `--output-format json-minified`.
65
+ 3. **Unit Tests:** Added unit tests for `MinifiedJsonFormatter` in `test/__tests__/unit/services/formatters.test.ts`.
66
+ 4. **E2E Tests:** Added E2E tests for the `json-minified` format in `test/__tests__/e2e/format.test.ts`.
67
+ 5. **Test Helpers:** Updated `StartServerOptions` type in `test/utils/mcp-test-helpers.ts`.
68
+ 6. **Memory Bank Update (✓):** Updated `techContext.md`, `systemPatterns.md`, `progress.md`, and `activeContext.md`.
69
+
70
+ ### Remote Spec & Swagger v2.0 Support (✓)
71
+
72
+ 1. **Remote Loading:** Added support for loading specifications via HTTP/HTTPS URLs using `swagger2openapi` in `SpecLoaderService`.
73
+ 2. **Swagger v2.0 Conversion:** Added support for automatically converting Swagger v2.0 specifications to OpenAPI v3.0 using `swagger2openapi` in `SpecLoaderService`.
74
+ 3. **Dependency Change:** Replaced `@apidevtools/swagger-parser` with `swagger2openapi`. Added `@types/swagger2openapi`. Reinstalled `openapi-types`.
75
+ 4. **Configuration:** Confirmed configuration uses CLI arguments (`<path-or-url-to-spec>`).
76
+ 5. **Testing:**
77
+ - Updated `SpecLoaderService` unit tests (`spec-loader.test.ts`) to mock `swagger2openapi` and cover new scenarios (local/remote, v2/v3). Fixed linting/hoisting issues in mocks.
78
+ - Created new E2E test file (`spec-loading.test.ts`) to verify loading from local v2 and remote v3 sources. Added necessary helpers and fixed linting issues.
79
+ - Added v2.0 test fixture (`sample-v2-api.json`).
80
+ 6. **Memory Bank Update (✓):** Updated `productContext.md`, `techContext.md`, `systemPatterns.md`, `progress.md`, and `activeContext.md` to reflect these changes.
81
+
82
+ ### Major Refactor (Previously Completed - ✓)
83
+
84
+ 1. **Unified URI Structure:** Implemented consistent URIs based on OpenAPI spec hierarchy (`openapi://{field}`, `openapi://paths/...`, `openapi://components/...`).
85
+ 2. **OOP Rendering Layer:** Created `Renderable*` classes for modular rendering logic (`src/rendering/`).
86
+ 3. **Refactored Handlers:** Replaced old handlers with new, focused handlers (`src/handlers/`).
87
+ - `TopLevelFieldHandler`
88
+ - `PathItemHandler`
89
+ - `OperationHandler`
90
+ - `ComponentMapHandler`
91
+ - `ComponentDetailHandler`
92
+ 4. **Shared Utilities:** Extracted common logic into `src/rendering/utils.ts` and `src/handlers/handler-utils.ts`.
93
+ 5. **Multi-Value Handling:** Correctly implemented handling for `*` variables (`method*`, `name*`).
94
+ 6. **Testing:**
95
+ - Added/passed unit tests for all `Renderable*` classes.
96
+ - Added/passed E2E tests for the new URI structure using `complex-endpoint.json`.
97
+ 7. **Documentation:** Updated `systemPatterns.md` and `progress.md`.
98
+ 8. **Archived Old Code:** Moved previous implementations to `local-docs/old-implementation/`.
99
+ 9. **URI Generation Refactor (✓):**
100
+ - Created centralized URI builder utility (`src/utils/uri-builder.ts`).
101
+ - Updated `$ref` transformation (`src/services/reference-transform.ts`) to use the builder and support all component types (`openapi://components/{type}/{name}`).
102
+ - Updated hint generation (`src/rendering/utils.ts`, `components.ts`, `path-item.ts`) to use the builder.
103
+ - Corrected path encoding in builder (removed leading slash encoding).
104
+ - Updated relevant unit tests (`uri-builder.test.ts`, `reference-transform.test.ts`, `path-item.test.ts`).
105
+ - Fixed `RenderablePathItem` instantiation in handlers (`path-item-handler.ts`, `operation-handler.ts`).
106
+ 10. **Security Fix (✓):** Resolved `security/detect-object-injection` warnings by implementing Map-based validation helpers (`getValidatedPathItem`, `getValidatedOperations`, `getValidatedComponentMap`, `getValidatedComponentDetails`) in `handler-utils.ts` and refactoring handlers (`operation-handler`, `path-item-handler`, `component-map-handler`, `component-detail-handler`) and rendering classes (`RenderablePaths`, `RenderableComponents`, `RenderableComponentMap`) to use safe access patterns inspired by the old implementation.
107
+
108
+ ## Implementation Status
109
+
110
+ - Server now loads OpenAPI v3.0 and Swagger v2.0 specs from local files or remote URLs.
111
+ - Swagger v2.0 specs are automatically converted to v3.0.
112
+ - Internal references are transformed to MCP URIs.
113
+ - Added `json-minified` output format option.
114
+ - Server name is now dynamically set based on the loaded spec's `info.title`.
115
+ - **New:** Automated versioning and release process implemented using `semantic-release`.
116
+ - **New:** CI workflow adapted for Node 22, uses `just` for checks, and includes a release job.
117
+ - Dependencies correctly categorized (`swagger2openapi` in `dependencies`, types in `devDependencies`).
118
+ - Resource completion logic implemented.
119
+ - Dynamic server name implemented.
120
+ - Minified JSON output format added.
121
+ - Remote spec loading and Swagger v2.0 conversion support added.
122
+ - Core resource exploration functionality remains operational.
123
+ - Unit tests for `SpecLoaderService` and `Formatters` are updated.
124
+ - E2E tests cover basic loading scenarios, output formats, resource exploration, and resource completion.
125
+
126
+ ## Next Actions / Immediate Focus
127
+
128
+ 1. **README Update:** Enhance `README.md` with:
129
+ - Details about the automated release process and the requirement for Conventional Commits.
130
+ - Instructions for setting up the `NPM_TOKEN` secret for publishing.
131
+ - Updated usage examples and explanations (including `json-minified` format).
132
+ 2. **Handler Unit Tests:** Implement comprehensive unit tests for each handler class (`TopLevelFieldHandler`, `PathItemHandler`, etc.), mocking `SpecLoaderService` and `IFormatter`.
133
+ 3. **Refactor Helpers:** Consolidate duplicated helper functions (`formatResults`, `isOpenAPIV3`) fully into `handler-utils.ts` and remove from individual handlers.
134
+ 4. **Code Cleanup:** Address remaining TODOs (e.g., checking warnings in `spec-loader.ts`) and minor ESLint warnings.
135
+
136
+ ## Future Considerations (Post Immediate Actions)
137
+
138
+ - Implement reference traversal/resolution service.
139
+ - Enhance support for all component types.
@@ -0,0 +1,39 @@
1
+ # Product Context
2
+
3
+ ## Problem Statement
4
+
5
+ When working with large OpenAPI specifications, loading the entire spec into an LLM's context:
6
+
7
+ 1. Consumes excessive tokens due to fully resolved references
8
+ 2. May confuse the LLM with too much information
9
+ 3. Makes it difficult to focus on specific parts of the API
10
+ 4. Duplicates schema information across multiple endpoints
11
+
12
+ ## Solution
13
+
14
+ An MCP server that:
15
+
16
+ 1. Loads OpenAPI v3.0 and Swagger v2.0 specs from local files or remote URLs.
17
+ 2. Automatically converts Swagger v2.0 specs to OpenAPI v3.0.
18
+ 3. Transforms internal references (`#/components/...`) to token-efficient MCP URIs.
19
+ 4. Provides selective access to specific parts of the spec via MCP resources.
20
+ 5. Returns information in token-efficient formats (text lists, JSON/YAML details).
21
+ 6. Makes it easy for LLMs to explore API structures without loading the entire spec.
22
+
23
+ ## User Experience Goals
24
+
25
+ 1. Easy installation via npm.
26
+ 2. Simple configuration via a single command-line argument (path or URL).
27
+ 3. Intuitive resource URIs for exploring API parts.
28
+ 4. Clear and consistent response formats.
29
+
30
+ ## Usage Workflow
31
+
32
+ 1. User installs MCP server via `npm install -g mcp-openapi-schema-explorer`.
33
+ 2. User runs the server providing the path or URL to the spec file via CLI argument (e.g., `mcp-openapi-schema-explorer ./spec.yaml` or `mcp-openapi-schema-explorer https://.../spec.json`).
34
+ 3. Server loads the spec (from file or URL), converts v2.0 to v3.0 if necessary, and transforms internal references to MCP URIs.
35
+ 4. LLM explores API structure through exposed resources:
36
+ - List paths, components, methods.
37
+ - View details for info, operations, components, etc.
38
+ - Follow transformed reference URIs (`openapi://components/...`) to view component details without loading the whole spec initially.
39
+ 5. Server restarts required if the source specification file/URL content changes.
@@ -0,0 +1,141 @@
1
+ # Project Progress
2
+
3
+ ## Completed Features
4
+
5
+ ### Core Refactoring & New Resource Structure (✓)
6
+
7
+ 1. **Unified URI Structure:** Implemented a consistent URI structure based on OpenAPI spec hierarchy:
8
+ - `openapi://{field}`: Access top-level fields (info, servers, tags) or list paths/component types.
9
+ - `openapi://paths/{path}`: List methods for a specific path.
10
+ - `openapi://paths/{path}/{method*}`: Get details for one or more operations.
11
+ - `openapi://components/{type}`: List names for a specific component type.
12
+ - `openapi://components/{type}/{name*}`: Get details for one or more components.
13
+ 2. **OOP Rendering Layer:** Introduced `Renderable*` classes (`RenderableDocument`, `RenderablePaths`, `RenderablePathItem`, `RenderableComponents`, `RenderableComponentMap`) to encapsulate rendering logic.
14
+ - Uses `RenderContext` and intermediate `RenderResultItem` structure.
15
+ - Supports token-efficient text lists and formatted detail views (JSON/YAML).
16
+ 3. **Refactored Handlers:** Created new, focused handlers for each URI pattern:
17
+ - `TopLevelFieldHandler`
18
+ - `PathItemHandler`
19
+ - `OperationHandler`
20
+ - `ComponentMapHandler`
21
+ - `ComponentDetailHandler`
22
+ - Uses shared utilities (`handler-utils.ts`).
23
+ 4. **Multi-Value Support:** Correctly handles `*` variables (`method*`, `name*`) passed as arrays by the SDK.
24
+ 5. **Testing:**
25
+ - Added unit tests for all new `Renderable*` classes.
26
+ - Added unit tests for all new handler classes.
27
+ - Added E2E tests covering the new URI structure and functionality using `complex-endpoint.json`.
28
+ 6. **Archived Old Code:** Moved previous handler/test implementations to `local-docs/old-implementation/`.
29
+
30
+ ### Previous Features (Now Integrated/Superseded)
31
+
32
+ - Schema Listing (Superseded by `openapi://components/schemas`)
33
+ - Schema Details (Superseded by `openapi://components/schemas/{name*}`)
34
+ - Endpoint Details (Superseded by `openapi://paths/{path}/{method*}`)
35
+ - Endpoint List (Superseded by `openapi://paths`)
36
+
37
+ ### Remote Spec & Swagger v2.0 Support (✓)
38
+
39
+ 1. **Remote Loading:** Added support for loading specifications via HTTP/HTTPS URLs using `swagger2openapi`.
40
+ 2. **Swagger v2.0 Conversion:** Added support for automatically converting Swagger v2.0 specifications to OpenAPI v3.0 using `swagger2openapi`.
41
+ 3. **Dependency Change:** Replaced `@apidevtools/swagger-parser` with `swagger2openapi` for loading and conversion.
42
+ 4. **Configuration Update:** Confirmed and documented that configuration uses CLI arguments (`<path-or-url-to-spec>`) instead of environment variables.
43
+ 5. **Testing:**
44
+ - Updated `SpecLoaderService` unit tests to mock `swagger2openapi` and cover new scenarios (local/remote, v2/v3).
45
+ - Created new E2E test file (`spec-loading.test.ts`) to verify loading from local v2 and remote v3 sources.
46
+ - Added v2.0 test fixture (`sample-v2-api.json`).
47
+
48
+ ## Technical Features (✓)
49
+
50
+ ### Codebase Organization (Updated)
51
+
52
+ 1. File Structure
53
+
54
+ - `src/handlers/`: Contains individual handlers and `handler-utils.ts`.
55
+ - `src/rendering/`: Contains `Renderable*` classes, `types.ts`, `utils.ts`.
56
+ - `src/services/`: Updated `spec-loader.ts` to use `swagger2openapi`. Added `formatters.ts`.
57
+ - `src/`: `index.ts`, `config.ts`, `types.ts`.
58
+ - `test/`: Updated unit tests (`spec-loader.test.ts`, `formatters.test.ts`), added new E2E test (`spec-loading.test.ts`), added v2 fixture. Updated `format.test.ts`. Updated `mcp-test-helpers.ts`.
59
+ - `local-docs/old-implementation/`: Archived previous code.
60
+
61
+ 2. Testing Structure
62
+
63
+ - Unit tests for rendering classes (`test/__tests__/unit/rendering/`).
64
+ - Unit tests for handlers (`test/__tests__/unit/handlers/`).
65
+ - Unit tests for services (`spec-loader.test.ts`, `reference-transform.test.ts`, `formatters.test.ts`).
66
+ - E2E tests (`refactored-resources.test.ts`, `spec-loading.test.ts`, `format.test.ts`). Added tests for `json-minified`.
67
+ - Fixtures (`test/fixtures/`, including v2 and v3).
68
+ - Test utils (`test/utils/`). Updated `StartServerOptions` type.
69
+
70
+ 3. Type System
71
+
72
+ - OpenAPI v3 types.
73
+ - `RenderableSpecObject`, `RenderContext`, `RenderResultItem` interfaces.
74
+ - `FormattedResultItem` type for handler results.
75
+ - `OutputFormat` type updated.
76
+ - `IFormatter` interface.
77
+
78
+ 4. Error Handling
79
+ - Consistent error handling via `createErrorResult` and `formatResults`.
80
+ - Errors formatted as `text/plain`.
81
+
82
+ ### Reference Transformation (✓ - Updated)
83
+
84
+ - Centralized URI generation logic in `src/utils/uri-builder.ts`.
85
+ - `ReferenceTransformService` now correctly transforms all `#/components/...` refs to `openapi://components/{type}/{name}` using the URI builder.
86
+ - Path encoding corrected to remove leading slashes before encoding.
87
+ - Unit tests updated and passing for URI builder and transformer.
88
+
89
+ ### Output Format Enhancement (✓)
90
+
91
+ - Added `json-minified` output format option (`--output-format json-minified`).
92
+ - Implemented `MinifiedJsonFormatter` in `src/services/formatters.ts`.
93
+ - Updated configuration (`src/config.ts`) to accept the new format.
94
+ - Added unit tests for the new formatter (`test/__tests__/unit/services/formatters.test.ts`).
95
+ - Added E2E tests (`test/__tests__/e2e/format.test.ts`) to verify the new format.
96
+ - Updated test helper types (`test/utils/mcp-test-helpers.ts`).
97
+
98
+ ### Dynamic Server Name (✓)
99
+
100
+ - Modified `src/index.ts` to load the OpenAPI spec before server initialization.
101
+ - Extracted `info.title` from the loaded spec.
102
+ - Set the `McpServer` name dynamically using the format `Schema Explorer for {title}` with a fallback.
103
+
104
+ ### Dependency Cleanup & Release Automation (✓)
105
+
106
+ 1. **Dependency Correction:** Correctly categorized runtime (`swagger2openapi`) vs. development (`@types/*`) dependencies in `package.json`. Removed unused types.
107
+ 2. **Automated Releases:** Implemented `semantic-release` with conventional commit analysis, changelog generation, npm publishing, and GitHub releases.
108
+ 3. **Dynamic Versioning:** Server version is now dynamically injected via `src/version.ts`, which is generated during the release process by `semantic-release` using `scripts/generate-version.js`. A default version file is tracked in Git for local builds.
109
+ 4. **CI Workflow:** Updated `.github/workflows/ci.yml` to use Node 22, remove Docker Compose, use `just` for running checks (`just all`, `just security`), and added a `release` job to automate publishing on pushes to `main`.
110
+
111
+ ## Planned Features (⏳)
112
+
113
+ - **Handler Unit Tests:** Complete unit tests for all new handlers (mocking services).
114
+ - **Refactor Helpers:** Move duplicated helpers (`formatResults`, `isOpenAPIV3`) from handlers to `handler-utils.ts`. (Deferred during refactor).
115
+ - **Security Validation (✓):** Implemented Map-based validation helpers in `handler-utils.ts` and refactored handlers/rendering classes to resolve object injection warnings.
116
+ - **Completion Logic (✓):** Implemented `complete` callbacks in `ResourceTemplate` definitions within `src/index.ts` for `{field}`, `{path}`, `{method*}`, `{type}`, and conditionally for `{name*}`. Added E2E tests using `client.complete()`.
117
+ - **Reference Traversal:** Service to resolve `$ref` URIs (e.g., follow `openapi://components/schemas/Task` from an endpoint detail).
118
+ - **Enhanced Component Support:** Ensure all component types listed in `VALID_COMPONENT_TYPES` are fully handled if present in spec. (Reference transformation now supports all types).
119
+ - **Parameter Validation:** Add validation logic if needed. (Current Map-based approach handles key validation).
120
+ - **Further Token Optimizations:** Explore more ways to reduce token usage in list/detail views.
121
+
122
+ ## Technical Improvements (Ongoing)
123
+
124
+ 1. Code Quality
125
+
126
+ - OOP design for rendering.
127
+ - Clear separation of concerns (Rendering vs. Handling vs. Services).
128
+ - Improved type safety in rendering/handling logic.
129
+
130
+ 2. Testing
131
+
132
+ - Unit tests added for rendering logic.
133
+ - Unit tests updated for URI builder, reference transformer, and path item rendering.
134
+ - E2E tests updated for new structure and complex fixture. Added tests for resource completion.
135
+ - Unit tests for `SpecLoaderService` updated for `swagger2openapi`.
136
+ - CI workflow updated to use `just` and includes automated release job.
137
+
138
+ 3. API Design
139
+ - New URI structure implemented, aligned with OpenAPI spec.
140
+ - Consistent list/detail pattern via rendering layer.
141
+ - Server now accepts remote URLs and Swagger v2.0 specs via CLI argument.
@@ -0,0 +1,50 @@
1
+ # OpenAPI Schema Explorer MCP Server
2
+
3
+ ## Project Overview
4
+
5
+ Building an MCP server that allows exploration of OpenAPI specification files in a selective, token-efficient manner.
6
+
7
+ ## Core Requirements (✓)
8
+
9
+ 1. Allow loading and exploring OpenAPI spec files without consuming excessive LLM tokens
10
+ - Token-efficient plain text listings
11
+ - JSON format for detailed views
12
+ - Error handling without excessive details
13
+ 2. Expose key parts of OpenAPI specs through MCP resources
14
+ - Endpoint details with full operation info
15
+ - Multiple values support for batch operations
16
+ - Resource completion support (✓)
17
+ 3. Support local OpenAPI specification files
18
+ - OpenAPI v3.0 support
19
+ - Local file loading
20
+ - Error handling for invalid specs
21
+ 4. Provide test coverage with Jest
22
+ - Full unit test coverage
23
+ - E2E test coverage
24
+ - Type-safe test implementation
25
+
26
+ ## Future Extensions (Out of Scope)
27
+
28
+ - Remote OpenAPI specs
29
+ - Different specification formats
30
+ - Search functionality
31
+
32
+ ## Technical Constraints (✓)
33
+
34
+ - Built with TypeScript MCP SDK
35
+ - Published to npm
36
+ - Comprehensive test coverage
37
+ - Optimized for testability and extensibility
38
+
39
+ ## Project Boundaries
40
+
41
+ - Initial focus on local OpenAPI spec files only
42
+ - Focus on most important parts: endpoints and type definitions
43
+ - Real-time spec updates are out of scope (server restart required for updates)
44
+
45
+ ## Next Optimizations
46
+
47
+ - YAML output format for improved token efficiency
48
+ - $ref resolution using URI links
49
+ - Parameter validation implementation
50
+ - Enhanced documentation support
@@ -0,0 +1,224 @@
1
+ # System Patterns
2
+
3
+ ## Architecture Overview
4
+
5
+ ```mermaid
6
+ graph TD
7
+ CliArg[CLI Argument (Path/URL)] --> Config[src/config.ts]
8
+ Config --> Server[MCP Server]
9
+
10
+ CliArg --> SpecLoader[Spec Loader Service]
11
+ SpecLoader -- Uses --> S2OLib[swagger2openapi Lib]
12
+ SpecLoader --> Transform[Reference Transform Service]
13
+ Transform --> Handlers[Resource Handlers]
14
+
15
+ Server --> Handlers
16
+
17
+ subgraph Services
18
+ SpecLoader
19
+ Transform
20
+ S2OLib
21
+ end
22
+
23
+ subgraph Handlers
24
+ TopLevelFieldHandler[TopLevelField Handler (openapi://{field})]
25
+ PathItemHandler[PathItem Handler (openapi://paths/{path})]
26
+ OperationHandler[Operation Handler (openapi://paths/{path}/{method*})]
27
+ ComponentMapHandler[ComponentMap Handler (openapi://components/{type})]
28
+ ComponentDetailHandler[ComponentDetail Handler (openapi://components/{type}/{name*})]
29
+ end
30
+
31
+ subgraph Formatters
32
+ JsonFormatter[Json Formatter]
33
+ YamlFormatter[Yaml Formatter]
34
+ MinifiedJsonFormatter[Minified Json Formatter]
35
+ end
36
+
37
+ subgraph Rendering (OOP)
38
+ RenderableDocument[RenderableDocument]
39
+ RenderablePaths[RenderablePaths]
40
+ RenderablePathItem[RenderablePathItem]
41
+ RenderableComponents[RenderableComponents]
42
+ RenderableComponentMap[RenderableComponentMap]
43
+ RenderUtils[Rendering Utils]
44
+ end
45
+
46
+ Handlers --> Rendering
47
+ SpecLoader --> Rendering
48
+
49
+ subgraph Utils
50
+ UriBuilder[URI Builder (src/utils)]
51
+ end
52
+
53
+ UriBuilder --> Transform
54
+ UriBuilder --> RenderUtils
55
+ ```
56
+
57
+ ## Component Structure
58
+
59
+ ### Services Layer
60
+
61
+ - **SpecLoaderService (`src/services/spec-loader.ts`):**
62
+ - Uses `swagger2openapi` library.
63
+ - Loads specification from local file path or remote URL provided via CLI argument.
64
+ - Handles parsing of JSON/YAML.
65
+ - Automatically converts Swagger v2.0 specs to OpenAPI v3.0 objects.
66
+ - Provides the resulting OpenAPI v3.0 document object.
67
+ - Handles errors during loading/conversion.
68
+ - **ReferenceTransformService (`src/services/reference-transform.ts`):**
69
+ - Takes the OpenAPI v3.0 document from `SpecLoaderService`.
70
+ - Traverses the document and transforms internal references (e.g., `#/components/schemas/MySchema`) into MCP URIs (e.g., `openapi://components/schemas/MySchema`).
71
+ - Uses `UriBuilder` utility for consistent URI generation.
72
+ - Returns the transformed OpenAPI v3.0 document.
73
+ - **Formatters (`src/services/formatters.ts`):**
74
+ - Provide implementations for different output formats (JSON, YAML, Minified JSON).
75
+ - Used by handlers to serialize detail view responses.
76
+ - `IFormatter` interface defines `format()` and `getMimeType()`.
77
+ - `createFormatter` function instantiates the correct formatter based on `OutputFormat` type (`json`, `yaml`, `json-minified`).
78
+
79
+ ### Rendering Layer (OOP)
80
+
81
+ - **Renderable Classes:** Wrapper classes (`RenderableDocument`, `RenderablePaths`, `RenderablePathItem`, `RenderableComponents`, `RenderableComponentMap`) implement `RenderableSpecObject` interface.
82
+ - **Interface:** `RenderableSpecObject` defines `renderList()` and `renderDetail()` methods returning `RenderResultItem[]`.
83
+ - **RenderResultItem:** Intermediate structure holding data (`unknown`), `uriSuffix`, `isError?`, `errorText?`, `renderAsList?`.
84
+ - **RenderContext:** Passed to render methods, contains `formatter` and `baseUri`.
85
+ - **Utils:** Helper functions (`getOperationSummary`, `generateListHint`, `createErrorResult`) in `src/rendering/utils.ts`. `generateListHint` now uses centralized URI builder logic.
86
+
87
+ ### Handler Layer
88
+
89
+ - **Structure:** Separate handlers for each distinct URI pattern/resource type.
90
+ - **Responsibilities:**
91
+ - Parse URI variables provided by SDK.
92
+ - Load/retrieve the transformed spec via `SpecLoaderService`.
93
+ - Instantiate appropriate `Renderable*` classes.
94
+ - Invoke the correct rendering method (`renderList` or a specific detail method like `renderTopLevelFieldDetail`, `renderOperationDetail`, `renderComponentDetail`).
95
+ - Format the `RenderResultItem[]` using `formatResults` from `src/handlers/handler-utils.ts`.
96
+ - Construct the final `{ contents: ... }` response object.
97
+ - Instantiate `RenderablePathItem` correctly with raw path and built suffix.
98
+ - **Handlers:**
99
+ - `TopLevelFieldHandler`: Handles `openapi://{field}`. Delegates list rendering for `paths`/`components` to `RenderablePaths`/`RenderableComponents`. Renders details for other fields (`info`, `servers`, etc.) via `RenderableDocument.renderTopLevelFieldDetail`.
100
+ - `PathItemHandler`: Handles `openapi://paths/{path}`. Uses `RenderablePathItem.renderList` to list methods. Instantiates `RenderablePathItem` with raw path and built suffix.
101
+ - `OperationHandler`: Handles `openapi://paths/{path}/{method*}`. Uses `RenderablePathItem.renderOperationDetail` for operation details. Handles multi-value `method` variable. Instantiates `RenderablePathItem` with raw path and built suffix.
102
+ - `ComponentMapHandler`: Handles `openapi://components/{type}`. Uses `RenderableComponentMap.renderList` to list component names.
103
+ - `ComponentDetailHandler`: Handles `openapi://components/{type}/{name*}`. Uses `RenderableComponentMap.renderComponentDetail` for component details. Handles multi-value `name` variable.
104
+ - **Utils:** Shared functions (`formatResults`, `isOpenAPIV3`, `FormattedResultItem` type, validation helpers) in `src/handlers/handler-utils.ts`.
105
+
106
+ ### Utilities Layer
107
+
108
+ - **URI Builder (`src/utils/uri-builder.ts`):**
109
+ - Centralized functions for building full URIs (`openapi://...`) and URI suffixes.
110
+ - Handles encoding of path components (removing leading slash first).
111
+ - Used by `ReferenceTransformService` and the rendering layer (`generateListHint`, `Renderable*` classes) to ensure consistency.
112
+
113
+ ### Configuration Layer (`src/config.ts`)
114
+
115
+ - Parses command-line arguments.
116
+ - Expects a single required argument: the path or URL to the specification file.
117
+ - Supports an optional `--output-format` argument (`json`, `yaml`, `json-minified`).
118
+ - Validates arguments and provides usage instructions on error.
119
+ - Creates the `ServerConfig` object used by the server.
120
+
121
+ ## Release Automation (`semantic-release`)
122
+
123
+ - **Configuration:** Defined in `.releaserc.json`.
124
+ - **Workflow:**
125
+ 1. `@semantic-release/commit-analyzer`: Determines release type from conventional commits.
126
+ 2. `@semantic-release/release-notes-generator`: Generates release notes.
127
+ 3. `@semantic-release/changelog`: Updates `CHANGELOG.md`.
128
+ 4. `@semantic-release/npm`: Updates `version` in `package.json`.
129
+ 5. `@semantic-release/exec`: Runs `scripts/generate-version.js` to create/update `src/version.ts` with the new version.
130
+ 6. `@semantic-release/git`: Commits `package.json`, `package-lock.json`, `CHANGELOG.md`, and `src/version.ts`. Creates Git tag.
131
+ 7. `@semantic-release/github`: Creates GitHub Release.
132
+ - **Trigger:** Executed by the `release` job in the GitHub Actions workflow (`.github/workflows/ci.yml`) on pushes to the `main` branch.
133
+ - **Versioning:** The server version in `src/index.ts` is dynamically imported from the generated `src/version.ts`. A default `src/version.ts` (with `0.0.0-dev`) is kept in the repository for local builds.
134
+
135
+ ## Resource Design Patterns
136
+
137
+ ### URI Structure (Revised)
138
+
139
+ - Implicit List/Detail based on path depth.
140
+ - Aligned with OpenAPI specification structure.
141
+ - **Templates:**
142
+ - `openapi://{field}`: Top-level field details (info, servers) or list trigger (paths, components).
143
+ - `openapi://paths/{path}`: List methods for a specific path.
144
+ - `openapi://paths/{path}/{method*}`: Operation details (supports multiple methods).
145
+ - `openapi://components/{type}`: List names for a specific component type.
146
+ - `openapi://components/{type}/{name*}`: Component details (supports multiple names).
147
+ - **Completions:**
148
+ - Defined directly in `src/index.ts` within `ResourceTemplate` definitions passed to `server.resource()`.
149
+ - Uses the `transformedSpec` object loaded before server initialization.
150
+ - Provides suggestions for `{field}`, `{path}`, `{method*}`, `{type}`.
151
+ - Provides suggestions for `{name*}` _only_ if the spec contains exactly one component type.
152
+ - **Reference URIs (Corrected):**
153
+ - Internal `$ref`s like `#/components/schemas/MySchema` are transformed by `ReferenceTransformService` into resolvable MCP URIs: `openapi://components/schemas/MySchema`.
154
+ - This applies to all component types under `#/components/`.
155
+ - External references remain unchanged.
156
+
157
+ ### Response Format Patterns
158
+
159
+ 1. **Token-Efficient Lists:**
160
+ - `text/plain` format used for all list views (`openapi://paths`, `openapi://components`, `openapi://paths/{path}`, `openapi://components/{type}`).
161
+ - Include hints for navigating to detail views, generated via `generateListHint` using the centralized URI builder.
162
+ - `openapi://paths` format: `METHOD1 METHOD2 /path`
163
+ - `openapi://paths/{path}` format: `METHOD: Summary/OpId`
164
+ - `openapi://components` format: `- type`
165
+ - `openapi://components/{type}` format: `- name`
166
+ 2. **Detail Views:**
167
+ - Use configured formatter (JSON/YAML/Minified JSON via `IFormatter`).
168
+ - Handled by `openapi://{field}` (for non-structural fields), `openapi://paths/{path}/{method*}`, `openapi://components/{type}/{name*}`.
169
+ 3. **Error Handling:**
170
+ - Handlers catch errors and use `createErrorResult` utility.
171
+ - `formatResults` utility formats errors into `FormattedResultItem` with `isError: true`, `mimeType: 'text/plain'`, and error message in `text`.
172
+ 4. **Type Safety:**
173
+ - Strong typing with OpenAPI v3 types.
174
+ - `Renderable*` classes encapsulate type-specific logic.
175
+ - `isOpenAPIV3` type guard used in handlers.
176
+
177
+ ## Extension Points
178
+
179
+ 1. Reference Transformers:
180
+
181
+ - AsyncAPI transformer
182
+ - GraphQL transformer
183
+ - Custom format transformers
184
+
185
+ 2. Resource Handlers:
186
+
187
+ - Schema resource handler
188
+ - Additional reference handlers
189
+ - Custom format handlers (via `IFormatter` interface)
190
+
191
+ 3. URI Resolution:
192
+
193
+ - Reference transformation service (`ReferenceTransformService`) handles converting `#/components/{type}/{name}` to `openapi://components/{type}/{name}` URIs during spec loading.
194
+ - Cross-resource linking is implicit via generated URIs in hints and transformed refs.
195
+ - External references are currently kept as-is.
196
+
197
+ 4. Validation:
198
+ - Parameter validation
199
+ - Reference validation
200
+ - Format-specific validation
201
+
202
+ ## Testing Strategy
203
+
204
+ 1. **Unit Tests:**
205
+ - `SpecLoaderService`: Mock `swagger2openapi` to test local/remote and v2/v3 loading logic, including error handling.
206
+ - `ReferenceTransformService`: Verify correct transformation of `#/components/...` refs to MCP URIs.
207
+ - Rendering Classes (`Renderable*`): Test list and detail rendering logic.
208
+ - Handlers: Mock services (`SpecLoaderService`, `IFormatter`) to test URI parsing and delegation to rendering classes.
209
+ - `UriBuilder`: Test URI encoding and generation.
210
+ 2. **E2E Tests:**
211
+ - Use `mcp-test-helpers` to start the server with different spec inputs.
212
+ - **`spec-loading.test.ts`:** Verify basic resource access (`info`, `paths`, `components`, specific component detail) works correctly when loading:
213
+ - Local Swagger v2.0 spec (`test/fixtures/sample-v2-api.json`).
214
+ - Remote OpenAPI v3.0 spec (e.g., Petstore URL).
215
+ - **`refactored-resources.test.ts`:** Continue to test detailed resource interactions (multi-value params, specific path/method/component combinations, errors) using the primary complex local v3 fixture (`complex-endpoint.json`).
216
+ - **`format.test.ts`:** Verify different output formats (JSON/YAML/Minified JSON) work as expected.
217
+ - **Completion Tests:** Added to `refactored-resources.test.ts` using `client.complete()` to verify completion logic.
218
+ 3. **Test Support:**
219
+ - Type-safe test utilities (`mcp-test-helpers`). Updated `StartServerOptions` to include `json-minified`.
220
+ - Test fixtures for v2.0 and v3.0 specs.
221
+ 4. **CI Integration (`.github/workflows/ci.yml`):**
222
+ - **`test` Job:** Runs on push/PR to `main`. Uses Node 22, installs `just`, runs `npm ci`, then `just all` (format, lint, build, test). Uploads coverage.
223
+ - **`security` Job:** Runs on push/PR to `main`. Uses Node 22, installs `just`, runs `npm ci`, then `just security` (audit, licenses). Runs CodeQL analysis separately.
224
+ - **`release` Job:** Runs _only_ on push to `main` after `test` and `security` pass. Checks out full history, installs dependencies, runs `npx semantic-release` using `GITHUB_TOKEN` and `NPM_TOKEN`.