Add authentication header support to GET, PUT, PATCH, DELETE API tools#193
Merged
Merged
Conversation
Co-authored-by: executeautomation <10337030+executeautomation@users.noreply.github.com>
Co-authored-by: executeautomation <10337030+executeautomation@users.noreply.github.com>
…inability Co-authored-by: executeautomation <10337030+executeautomation@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix API authentication issue with MCP Server
Add authentication header support to GET, PUT, PATCH, DELETE API tools
Dec 11, 2025
…uality This commit enhances the API authentication feature with better code quality, type safety, comprehensive testing, and complete documentation. ## Code Quality Improvements ### DRY Principle - Extract duplicate header building logic into `buildHeaders()` helper - Reduces code duplication across 5 request methods - Centralizes authentication logic for easier maintenance ### Type Safety - Add `BaseRequestArgs` interface for GET/DELETE requests - Add `RequestWithBodyArgs` interface for POST/PUT/PATCH requests - Replace `any` types with proper TypeScript interfaces ### Validation & Error Handling - Add `validateHeaders()` to ensure all header values are strings - Add console warnings when both token and Authorization header provided - Improve `parseJsonSafely()` with warning logs for debugging - Better error messages for invalid header values ## Testing Enhancements Added 8 new edge case tests: - Invalid header value validation (non-string values) - Token and Authorization header conflict warnings (GET, POST) - JSON parse failure handling with fallback - Empty headers object handling - Header validation for PUT, PATCH, DELETE methods **Test Results:** - ✅ All 150 tests passing (142 existing + 8 new) - ✅ Coverage increased: 88.37% for requests.ts (up from ~81%) - ✅ Zero TypeScript errors - ✅ Build successful ## Documentation Updates ### Supported Tools Documentation Updated `docs/docs/playwright-api/Supported-Tools.mdx`: - Added `token` and `headers` parameters to GET, PUT, PATCH, DELETE - Previously only POST had these documented - Consistent documentation across all API methods ### Usage Examples Enhanced `docs/docs/playwright-api/Examples.md` with: - Bearer token authentication examples - Basic authentication examples - API key authentication examples - Custom header examples - Token + headers combination examples ### CHANGELOG Added comprehensive entry documenting: - All new features (token, headers, validation) - Code quality improvements - Type safety additions - Test coverage improvements - Backward compatibility notes ## Technical Details - **Backward Compatible**: Existing API calls work unchanged - **Custom Headers Override**: Authorization header in `headers` overrides `token` - **Warning System**: Console warnings help developers debug conflicts - **Header Validation**: Prevents runtime errors from invalid types - **Improved Coverage**: API requests.ts now at 88.37% coverage ## Files Changed - `src/tools/api/requests.ts`: Refactored with helpers and types (+104 lines) - `src/__tests__/tools/api/requests.test.ts`: Added edge case tests (+156 lines) - `docs/docs/playwright-api/Supported-Tools.mdx`: Updated docs (+16 lines) - `docs/docs/playwright-api/Examples.md`: Added auth examples (+89 lines) - `CHANGELOG.md`: Comprehensive changelog entry (+32 lines) Total: +397 insertions, -29 deletions across 5 files
- Update package.json to version 1.0.11 - Update hardcoded version strings in src/index.ts and src/http-server.ts - Update CHANGELOG.md: Change [Unreleased] to [1.0.11] - 2024-12-11 - Add comprehensive v1.0.11 release notes to docs/docs/release.mdx - Document Bearer token authentication for all API methods - Document custom headers support - Highlight code quality improvements (DRY, Type Safety) - Detail testing enhancements (150 tests, 88.37% coverage) - Include usage examples and backward compatibility notes All tests passing (150/150) ✅ Build successful ✅
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Only POST requests supported authentication headers, causing 403 errors when making authenticated PUT/PATCH/DELETE/GET requests.
Changes
src/tools.ts): Added optionaltokenandheadersparameters to GET, PUT, PATCH, DELETE toolssrc/tools/api/requests.ts):parseJsonSafely()helper to handle JSON parsing with fallbackContent-Type: application/json), custom headers overridesrc/__tests__/tools/api/requests.test.ts): Added coverage for Bearer tokens, Basic auth, and custom headers across all methodsUsage
Bearer token:
Basic auth or custom headers:
Both parameters are optional; existing code without authentication continues to work unchanged.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
restful-booker.herokuapp.com/usr/local/bin/node node /tmp/test-real-api.mjs(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.