Skip to content

Commit 290e61c

Browse files
committed
Added https client support for backend calls and modularized server.ts to simplify
1 parent 38afa7c commit 290e61c

File tree

9 files changed

+766
-147
lines changed

9 files changed

+766
-147
lines changed

mcp-openapi/README.md

Lines changed: 147 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,13 @@ Options:
697697
--max-tool-name-length <number> Maximum length for generated tool names (default: "48")
698698
--max-request-size <size> Maximum size for JSON request bodies (default: "2mb")
699699
--max-response-size-mb <number> Maximum size for backend API responses in MB (default: "50")
700+
--https-client-ca <path> Path to CA certificate file for HTTPS backend APIs
701+
--https-client-cert <path> Path to client certificate file for HTTPS backend APIs
702+
--https-client-key <path> Path to client private key file for HTTPS backend APIs
703+
--https-client-pfx <path> Path to client PFX/PKCS12 file for HTTPS backend APIs
704+
--https-client-passphrase <pass> Passphrase for client private key
705+
--https-client-reject-unauthorized Reject self-signed certificates (default: true)
706+
--https-client-timeout <ms> HTTPS request timeout in milliseconds (default: "30000")
700707
--http Run in HTTP server mode instead of stdio (default: false)
701708
--https Enable HTTPS mode (requires certificate files)
702709
--https-port <number> Port for HTTPS server mode (default: "4443")
@@ -802,84 +809,86 @@ curl http://localhost:4000/info
802809
}
803810
```
804811

805-
## HTTPS Configuration
812+
## 🔒 MCP Server HTTPS Configuration
806813

807-
The server supports secure HTTPS connections with TLS encryption for production deployments:
814+
Configure the MCP OpenAPI server to serve MCP clients over secure HTTPS connections with TLS encryption:
808815

809-
### HTTPS Setup Options
816+
### MCP Server HTTPS Setup Options
810817

811818
**Option 1: Separate Key and Certificate Files**
812819
```bash
813-
# Generate or obtain SSL certificate files
814-
# Then run with separate key and cert files
820+
# Generate or obtain SSL certificate files for the MCP server
815821
mcp-openapi-server --https \
816-
--key-file ./certs/private.key \
817-
--cert-file ./certs/certificate.crt \
822+
--key-file ./certs/mcp-server.key \
823+
--cert-file ./certs/mcp-server.crt \
818824
--https-port 4443
819825
```
820826

821827
**Option 2: PFX/PKCS12 File**
822828
```bash
823-
# Use a single PFX file (common in Windows environments)
829+
# Use a single PFX file for the MCP server (common in Windows environments)
824830
mcp-openapi-server --https \
825-
--pfx-file ./certs/server.pfx \
831+
--pfx-file ./certs/mcp-server.pfx \
826832
--passphrase mypassword \
827833
--https-port 4443
828834
```
829835

830-
### HTTPS Examples
836+
### MCP Server HTTPS Examples
831837

832-
**Basic HTTPS with Banking Example:**
838+
**Development HTTPS Setup:**
833839
```bash
834-
# HTTPS mode with banking examples
840+
# Development HTTPS mode for secure MCP client connections
835841
mcp-openapi-server --https \
836-
--key-file ./certs/server.key \
837-
--cert-file ./certs/server.crt \
838-
--specs ./examples/specs \
839-
--config ./examples/mcp-config.json
842+
--key-file ./certs/dev-server.key \
843+
--cert-file ./certs/dev-server.crt \
844+
--https-port 4443 \
845+
--specs ./specs \
846+
--config ./mcp-config.json
840847
```
841848

842-
**HTTPS with Custom Configuration:**
849+
**Production HTTPS Setup:**
843850
```bash
844-
# Production HTTPS setup
851+
# Production HTTPS setup for serving MCP clients securely
845852
mcp-openapi-server --https \
846-
--pfx-file ./production/certs/api-server.pfx \
847-
--passphrase $CERT_PASSPHRASE \
853+
--pfx-file ./production/certs/mcp-server.pfx \
854+
--passphrase $MCP_SERVER_CERT_PASSPHRASE \
848855
--https-port 443 \
849-
--base-url https://api.production.com \
850-
--specs ./production/specs
856+
--specs ./production/specs \
857+
--config ./production/mcp-config.json
851858
```
852859

853-
### HTTPS Output
854-
When HTTPS is enabled, you'll see:
860+
### MCP Server HTTPS Output
861+
When MCP server HTTPS is enabled, you'll see:
855862
```
856863
🔒 MCP OpenAPI HTTPS Server running on port 4443
857864
📊 Health check: https://localhost:4443/health
858865
ℹ️ Server info: https://localhost:4443/info
859866
📋 Loaded 3 specs, 4 tools, 5 resources, 2 prompts
860867
```
861868

862-
### Certificate Requirements
869+
### MCP Server Certificate Requirements
863870

864-
**For `--key-file` and `--cert-file`:**
865-
- Private key file (`.key`, `.pem`)
866-
- Certificate file (`.crt`, `.cer`, `.pem`)
867-
- Both files must be readable by the server process
871+
**For MCP Server `--key-file` and `--cert-file`:**
872+
- Private key file (`.key`, `.pem`) for the MCP server
873+
- Certificate file (`.crt`, `.cer`, `.pem`) for the MCP server
874+
- Both files must be readable by the MCP server process
875+
- Certificate must be valid for the domain/IP where MCP clients will connect
868876

869-
**For `--pfx-file`:**
870-
- Single PKCS#12 file (`.pfx`, `.p12`) containing both key and certificate
877+
**For MCP Server `--pfx-file`:**
878+
- Single PKCS#12 file (`.pfx`, `.p12`) containing both key and certificate for the MCP server
871879
- Optional passphrase for encrypted PFX files
880+
- Certificate must be valid for the domain/IP where MCP clients will connect
872881

873-
### HTTPS + MCP Streaming Protocol
882+
### MCP Server HTTPS + MCP Streaming Protocol
874883

875-
HTTPS mode provides:
876-
-**Encrypted MCP sessions** - All JSON-RPC 2.0 messages encrypted with TLS
877-
-**Secure session management** - `Mcp-Session-Id` headers protected
878-
-**TLS certificate validation** - Standard HTTPS security
879-
-**Future secure SSE** - Ready for encrypted Server-Sent Events
880-
-**Production-ready** - Suitable for production MCP server deployments
884+
MCP Server HTTPS mode provides:
885+
-**Encrypted MCP client connections** - All JSON-RPC 2.0 messages from clients encrypted with TLS
886+
-**Secure session management** - `Mcp-Session-Id` headers protected in transit
887+
-**TLS certificate validation** - Standard HTTPS security for MCP clients
888+
-**Future secure SSE** - Ready for encrypted Server-Sent Events to clients
889+
-**Production-ready** - Suitable for production MCP server deployments serving multiple clients
881890

882-
### Troubleshooting HTTPS
891+
### Troubleshooting MCP Server HTTPS
883892

884893
**Certificate file not found:**
885894
```
@@ -1071,6 +1080,105 @@ The tests validate:
10711080

10721081
MIT License - see [LICENSE](LICENSE) file for details.
10731082

1083+
## 🔗 Backend API HTTPS Client Configuration
1084+
1085+
Configure the MCP OpenAPI server to connect securely to backend APIs over HTTPS with custom certificates and validation options.
1086+
1087+
### Backend API HTTPS Configuration Options
1088+
1089+
Add `httpsClient` configuration to your `mcp-config.json` for secure backend API connections:
1090+
1091+
```json
1092+
{
1093+
"baseUrl": "https://secure-backend-api.example.com",
1094+
"httpsClient": {
1095+
"rejectUnauthorized": true,
1096+
"timeout": 30000,
1097+
"keepAlive": true,
1098+
"certFile": "./certs/backend-client.crt",
1099+
"keyFile": "./certs/backend-client.key",
1100+
"caFile": "./certs/backend-ca.crt",
1101+
"passphrase": "optional-key-passphrase"
1102+
}
1103+
}
1104+
```
1105+
1106+
### Backend API HTTPS CLI Options
1107+
1108+
```bash
1109+
# Basic backend API HTTPS client options
1110+
mcp-openapi-server --base-url https://secure-backend-api.com \
1111+
--https-client-reject-unauthorized \
1112+
--https-client-timeout 30000
1113+
1114+
# Backend API client certificate authentication
1115+
mcp-openapi-server --base-url https://secure-backend-api.com \
1116+
--https-client-cert ./certs/backend-client.crt \
1117+
--https-client-key ./certs/backend-client.key
1118+
1119+
# Custom CA certificate for backend API
1120+
mcp-openapi-server --base-url https://secure-backend-api.com \
1121+
--https-client-ca ./certs/backend-ca.crt
1122+
1123+
# PFX/PKCS12 certificate for backend API
1124+
mcp-openapi-server --base-url https://secure-backend-api.com \
1125+
--https-client-pfx ./certs/backend-client.p12 \
1126+
--https-client-passphrase mypassword
1127+
```
1128+
1129+
### Backend API HTTPS Configuration Reference
1130+
1131+
| Option | Type | Default | Description |
1132+
|--------|------|---------|-------------|
1133+
| `rejectUnauthorized` | boolean | `true` | Reject self-signed certificates from backend APIs |
1134+
| `timeout` | number | `30000` | Request timeout for backend API calls in milliseconds |
1135+
| `keepAlive` | boolean | `true` | Use HTTP keep-alive for backend API connections |
1136+
| `certFile` | string | - | Path to client certificate file for backend API authentication |
1137+
| `keyFile` | string | - | Path to client private key file for backend API authentication |
1138+
| `pfxFile` | string | - | Path to PFX/PKCS12 file for backend API authentication (alternative to cert/key) |
1139+
| `caFile` | string | - | Path to custom CA certificate for backend API validation |
1140+
| `passphrase` | string | - | Passphrase for encrypted private key used with backend APIs |
1141+
1142+
### Backend API Certificate Types Supported
1143+
1144+
1. **Separate Certificate and Key Files for Backend API**
1145+
```json
1146+
{
1147+
"certFile": "./certs/backend-client.crt",
1148+
"keyFile": "./certs/backend-client.key"
1149+
}
1150+
```
1151+
1152+
2. **PFX/PKCS12 Bundle for Backend API**
1153+
```json
1154+
{
1155+
"pfxFile": "./certs/backend-client.p12",
1156+
"passphrase": "optional"
1157+
}
1158+
```
1159+
1160+
3. **Custom CA Certificate for Backend API**
1161+
```json
1162+
{
1163+
"caFile": "./certs/backend-ca.crt"
1164+
}
1165+
```
1166+
1167+
### Backend API HTTPS Validation Rules
1168+
1169+
- If `certFile` is provided for backend API authentication, `keyFile` is required
1170+
- If `keyFile` is provided for backend API authentication, `certFile` is required
1171+
- Cannot specify both `cert/key` files and `pfxFile` simultaneously for backend API authentication
1172+
- Encrypted certificate files require `passphrase` for backend API connections
1173+
- All certificate files must exist and be readable by the MCP server
1174+
- Timeout must be between 1000ms and 300000ms for backend API requests
1175+
1176+
### Backend API HTTPS Default Behavior
1177+
1178+
- **No HTTPS config**: Uses standard HTTP client with default Node.js settings for backend API connections
1179+
- **Partial config**: Validates configuration and fails startup if incomplete backend API HTTPS configuration
1180+
- **Complete config**: Enables HTTPS client with custom certificates and settings for secure backend API connections
1181+
10741182
## 🚧 Current Limitations
10751183

10761184
### Large Payload Handling

mcp-openapi/examples/mcp-config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"credentials": true
1010
},
1111
"maxResponseSizeMB": 50,
12+
"httpsClient": {
13+
"rejectUnauthorized": true,
14+
"timeout": 30000,
15+
"keepAlive": true
16+
},
1217
"overrides": [
1318
{
1419
"specId": "banking-payments",

mcp-openapi/src/cli.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ program
2121
.option('--max-tool-name-length <number>', 'Maximum length for generated tool names', '48')
2222
.option('--max-request-size <size>', 'Maximum size for JSON request bodies', '2mb')
2323
.option('--max-response-size-mb <number>', 'Maximum size for backend API responses in MB', '50')
24+
.option('--https-client-ca <path>', 'Path to CA certificate file for HTTPS backend APIs')
25+
.option('--https-client-cert <path>', 'Path to client certificate file for HTTPS backend APIs')
26+
.option('--https-client-key <path>', 'Path to client private key file for HTTPS backend APIs')
27+
.option('--https-client-pfx <path>', 'Path to client PFX/PKCS12 file for HTTPS backend APIs')
28+
.option('--https-client-passphrase <passphrase>', 'Passphrase for client private key')
29+
.option('--https-client-reject-unauthorized', 'Reject self-signed certificates (default: true)', true)
30+
.option('--https-client-timeout <ms>', 'HTTPS request timeout in milliseconds', '30000')
2431
.option('--http', 'Run in HTTP server mode instead of stdio', false)
2532
.option('--https', 'Enable HTTPS mode (requires --key-file and --cert-file or --pfx-file)', false)
2633
.option('--https-port <number>', 'Port for HTTPS server mode', '4443')
@@ -40,7 +47,17 @@ program
4047
...(options.maxToolNameLength && { maxToolNameLength: parseInt(options.maxToolNameLength) }),
4148
...(options.maxRequestSize && { maxRequestSize: options.maxRequestSize }),
4249
...(options.maxResponseSizeMb && { maxResponseSizeMB: parseInt(options.maxResponseSizeMb) }),
43-
// HTTPS options
50+
// HTTPS Client options
51+
...(options.httpsClientCa && { httpsClientCa: options.httpsClientCa }),
52+
...(options.httpsClientCert && { httpsClientCert: options.httpsClientCert }),
53+
...(options.httpsClientKey && { httpsClientKey: options.httpsClientKey }),
54+
...(options.httpsClientPfx && { httpsClientPfx: options.httpsClientPfx }),
55+
...(options.httpsClientPassphrase && { httpsClientPassphrase: options.httpsClientPassphrase }),
56+
...(options.httpsClientRejectUnauthorized !== undefined && {
57+
httpsClientRejectUnauthorized: options.httpsClientRejectUnauthorized
58+
}),
59+
...(options.httpsClientTimeout && { httpsClientTimeout: parseInt(options.httpsClientTimeout) }),
60+
// HTTPS Server options
4461
https: options.https,
4562
...(options.httpsPort && { httpsPort: parseInt(options.httpsPort) }),
4663
...(options.keyFile && { keyFile: options.keyFile }),

0 commit comments

Comments
 (0)