Compare commits

...
Sign in to create a new pull request.

21 commits

Author SHA1 Message Date
d1cb48f770
a
All checks were successful
Publish Any Commit / build (push) Successful in 30s
2025-05-18 18:01:29 +02:00
a63b93aa5c
wip
All checks were successful
Publish Any Commit / build (push) Successful in 25s
2025-05-18 17:32:42 +02:00
d8ce274506
wip
Some checks failed
Publish Any Commit / build (push) Failing after 36s
2025-05-18 17:23:07 +02:00
a7a76d3f17
wip
Some checks failed
Publish Any Commit / build (push) Failing after 18s
2025-05-18 17:18:21 +02:00
27907a4624
wip
Some checks failed
Publish Any Commit / build (push) Failing after 17s
2025-05-18 17:12:39 +02:00
0213c20d3d
update pnpm
Some checks failed
Publish Any Commit / build (push) Failing after 17s
2025-05-18 17:11:17 +02:00
4f6de14fbc
add packageManager
Some checks failed
Publish Any Commit / build (push) Failing after 15s
2025-05-18 17:10:14 +02:00
675dc6a760
fix tool path
Some checks failed
Publish Any Commit / build (push) Failing after 19s
2025-05-18 17:01:58 +02:00
8f83b18966
adjust ci
Some checks failed
Publish Any Commit / build (push) Failing after 3s
2025-05-18 17:00:50 +02:00
Will
7eecc9ca3f Update README.md
Move `env` into mcpServer configuration. The examples have it placed outside. 

If you don't pay attention, you'll end up wondering why you have empty `env` being passed through.
2025-05-15 07:26:39 +01:00
Glen Maddern
5199279ea7 0.1.5 2025-05-14 12:31:07 +01:00
Glen Maddern
b1dfa9fe5b Picking a default port based on the server hash 2025-05-14 12:31:07 +01:00
Glen Maddern
6f2399bbfb remove client info on conflict 2025-05-14 12:31:07 +01:00
Glen Maddern
e5cdf08bc8 Updated SDK version 2025-05-14 12:31:07 +01:00
Frédéric Barthelet
bd6df4222f Fix schema on clientInformation() 2025-05-14 11:53:08 +01:00
Frédéric Barthelet
b209d98074 Add port sourcing from existing client information 2025-05-14 11:53:08 +01:00
Glen Maddern
bd75a1cdf0 0.1.4 2025-05-12 15:37:49 +10:00
Tomer Zait
767549412f fix issue #64 2025-05-12 06:37:44 +01:00
Glen Maddern
46e3333416 0.1.3 2025-05-12 15:27:57 +10:00
Glen Maddern
63e02eef1c Use 127.0.0.1 everywhere _except_ as a redirect_uri for the client registration 2025-05-12 06:27:47 +01:00
Glen Maddern
45c1739b4c Adding (via mcp-remote <version>) to clientInfo.name on initialize 2025-05-12 06:27:37 +01:00
8 changed files with 98 additions and 47 deletions

View file

@ -1,11 +1,12 @@
name: Publish Any Commit name: Publish Any Commit
on: on:
workflow_dispatch:
pull_request: pull_request:
push: push:
branches: branches:
- "**" - "**"
tags: tags:
- "!**" - "v*"
jobs: jobs:
build: build:
@ -15,16 +16,18 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- run: corepack enable - name: Add git.kvant.cloud scope
- uses: actions/setup-node@v4 run: npm config set @kvant:registry=https://git.kvant.cloud/api/packages/${{ github.repository_owner }}/npm/
with:
node-version: 20
cache: "pnpm"
- name: Install dependencies - name: Login to git.kvant.cloud npm
run: pnpm install run: npm config set -- '//git.kvant.cloud/api/packages/${{ github.repository_owner }}/npm/:_authToken' "${{ secrets.PHOENIX_PACKAGE_WRITER_TOKEN }}"
- name: Setup pnpm & install
uses: https://github.com/wyvox/action-setup-pnpm@v3
with:
node-version: 22
- name: Build - name: Build
run: pnpm build run: pnpm build
- run: pnpm dlx pkg-pr-new publish --compact --bin - run: pnpm dlx publish --compact --bin

View file

@ -46,11 +46,11 @@ To bypass authentication, or to emit custom headers on all requests to your remo
"https://remote.mcp.server/sse", "https://remote.mcp.server/sse",
"--header", "--header",
"Authorization: Bearer ${AUTH_TOKEN}" "Authorization: Bearer ${AUTH_TOKEN}"
] ],
},
"env": { "env": {
"AUTH_TOKEN": "..." "AUTH_TOKEN": "..."
} }
},
} }
} }
``` ```
@ -65,11 +65,11 @@ To bypass authentication, or to emit custom headers on all requests to your remo
"https://remote.mcp.server/sse", "https://remote.mcp.server/sse",
"--header", "--header",
"Authorization:${AUTH_HEADER}" // note no spaces around ':' "Authorization:${AUTH_HEADER}" // note no spaces around ':'
] ],
}, "env": {
"env": {
"AUTH_HEADER": "Bearer <auth-token>" // spaces OK in env vars "AUTH_HEADER": "Bearer <auth-token>" // spaces OK in env vars
} }
},
``` ```
### Flags ### Flags

View file

@ -1,7 +1,6 @@
{ {
"name": "mcp-remote", "name": "@kvant/mcp-remote",
"version": "0.1.2", "version": "0.1.5",
"packageManager": "pnpm@8.15.1",
"description": "Remote proxy for Model Context Protocol, allowing local-only clients to connect to remote servers using oAuth", "description": "Remote proxy for Model Context Protocol, allowing local-only clients to connect to remote servers using oAuth",
"keywords": [ "keywords": [
"mcp", "mcp",
@ -32,8 +31,9 @@
"express": "^4.21.2", "express": "^4.21.2",
"open": "^10.1.0" "open": "^10.1.0"
}, },
"packageManager": "pnpm@10.11.0",
"devDependencies": { "devDependencies": {
"@modelcontextprotocol/sdk": "^1.10.2", "@modelcontextprotocol/sdk": "^1.11.2",
"@types/express": "^5.0.0", "@types/express": "^5.0.0",
"@types/node": "^22.13.10", "@types/node": "^22.13.10",
"prettier": "^3.5.3", "prettier": "^3.5.3",

10
pnpm-lock.yaml generated
View file

@ -16,8 +16,8 @@ importers:
version: 10.1.0 version: 10.1.0
devDependencies: devDependencies:
'@modelcontextprotocol/sdk': '@modelcontextprotocol/sdk':
specifier: ^1.10.2 specifier: ^1.11.2
version: 1.10.2 version: 1.11.2
'@types/express': '@types/express':
specifier: ^5.0.0 specifier: ^5.0.0
version: 5.0.0 version: 5.0.0
@ -211,8 +211,8 @@ packages:
'@jridgewell/trace-mapping@0.3.25': '@jridgewell/trace-mapping@0.3.25':
resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==} resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==}
'@modelcontextprotocol/sdk@1.10.2': '@modelcontextprotocol/sdk@1.11.2':
resolution: {integrity: sha512-rb6AMp2DR4SN+kc6L1ta2NCpApyA9WYNx3CrTSZvGxq9wH71bRur+zRqPfg0vQ9mjywR7qZdX2RGHOPq3ss+tA==} resolution: {integrity: sha512-H9vwztj5OAqHg9GockCQC06k1natgcxWQSRpQcPJf6i5+MWBzfKkRtxGbjQf0X2ihii0ffLZCRGbYV2f2bjNCQ==}
engines: {node: '>=18'} engines: {node: '>=18'}
'@pkgjs/parseargs@0.11.0': '@pkgjs/parseargs@0.11.0':
@ -1206,7 +1206,7 @@ snapshots:
'@jridgewell/resolve-uri': 3.1.2 '@jridgewell/resolve-uri': 3.1.2
'@jridgewell/sourcemap-codec': 1.5.0 '@jridgewell/sourcemap-codec': 1.5.0
'@modelcontextprotocol/sdk@1.10.2': '@modelcontextprotocol/sdk@1.11.2':
dependencies: dependencies:
content-type: 1.0.5 content-type: 1.0.5
cors: 2.8.5 cors: 2.8.5

View file

@ -151,7 +151,7 @@ async function runClient(
} }
// Parse command-line arguments and run the client // Parse command-line arguments and run the client
parseCommandLineArgs(process.argv.slice(2), 3333, 'Usage: npx tsx client.ts <https://server-url> [callback-port]') parseCommandLineArgs(process.argv.slice(2), 'Usage: npx tsx client.ts <https://server-url> [callback-port]')
.then(({ serverUrl, callbackPort, headers, transportStrategy }) => { .then(({ serverUrl, callbackPort, headers, transportStrategy }) => {
return runClient(serverUrl, callbackPort, headers, transportStrategy) return runClient(serverUrl, callbackPort, headers, transportStrategy)
}) })

View file

@ -1,9 +1,8 @@
import open from 'open' import open from 'open'
import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js' import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js'
import { import {
OAuthClientInformation,
OAuthClientInformationFull, OAuthClientInformationFull,
OAuthClientInformationSchema, OAuthClientInformationFullSchema,
OAuthTokens, OAuthTokens,
OAuthTokensSchema, OAuthTokensSchema,
} from '@modelcontextprotocol/sdk/shared/auth.js' } from '@modelcontextprotocol/sdk/shared/auth.js'
@ -37,7 +36,7 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
} }
get redirectUrl(): string { get redirectUrl(): string {
return `http://127.0.0.1:${this.options.callbackPort}${this.callbackPath}` return `http://localhost:${this.options.callbackPort}${this.callbackPath}`
} }
get clientMetadata() { get clientMetadata() {
@ -57,9 +56,9 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
* Gets the client information if it exists * Gets the client information if it exists
* @returns The client information or undefined * @returns The client information or undefined
*/ */
async clientInformation(): Promise<OAuthClientInformation | undefined> { async clientInformation(): Promise<OAuthClientInformationFull | undefined> {
// log('Reading client info') // log('Reading client info')
return readJsonFile<OAuthClientInformation>(this.serverUrlHash, 'client_info.json', OAuthClientInformationSchema) return readJsonFile<OAuthClientInformationFull>(this.serverUrlHash, 'client_info.json', OAuthClientInformationFullSchema)
} }
/** /**

View file

@ -3,6 +3,13 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js' import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js' import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js' import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import { OAuthClientInformationFull, OAuthClientInformationFullSchema } from '@modelcontextprotocol/sdk/shared/auth.js'
import { OAuthCallbackServerOptions } from './types'
import { getConfigFilePath, readJsonFile } from './mcp-auth-config'
import express from 'express'
import net from 'net'
import crypto from 'crypto'
import fs from 'fs/promises'
// Connection constants // Connection constants
export const REASON_AUTH_NEEDED = 'authentication-needed' export const REASON_AUTH_NEEDED = 'authentication-needed'
@ -10,10 +17,6 @@ export const REASON_TRANSPORT_FALLBACK = 'falling-back-to-alternate-transport'
// Transport strategy types // Transport strategy types
export type TransportStrategy = 'sse-only' | 'http-only' | 'sse-first' | 'http-first' export type TransportStrategy = 'sse-only' | 'http-only' | 'sse-first' | 'http-first'
import { OAuthCallbackServerOptions } from './types'
import express from 'express'
import net from 'net'
import crypto from 'crypto'
// Package version from package.json // Package version from package.json
export const MCP_REMOTE_VERSION = require('../../package.json').version export const MCP_REMOTE_VERSION = require('../../package.json').version
@ -32,14 +35,21 @@ export function mcpProxy({ transportToClient, transportToServer }: { transportTo
let transportToClientClosed = false let transportToClientClosed = false
let transportToServerClosed = false let transportToServerClosed = false
transportToClient.onmessage = (message) => { transportToClient.onmessage = (_message) => {
// @ts-expect-error TODO // TODO: fix types
const message = _message as any
log('[Local→Remote]', message.method || message.id) log('[Local→Remote]', message.method || message.id)
if (message.method === 'initialize') {
const { clientInfo } = message.params
if (clientInfo) clientInfo.name = `${clientInfo.name} (via mcp-remote ${MCP_REMOTE_VERSION})`
log(JSON.stringify(message, null, 2))
}
transportToServer.send(message).catch(onServerError) transportToServer.send(message).catch(onServerError)
} }
transportToServer.onmessage = (message) => { transportToServer.onmessage = (_message) => {
// @ts-expect-error TODO: fix this type // TODO: fix types
const message = _message as any
log('[Remote→Local]', message.method || message.id) log('[Remote→Local]', message.method || message.id)
transportToClient.send(message).catch(onClientError) transportToClient.send(message).catch(onClientError)
} }
@ -345,6 +355,27 @@ export function setupOAuthCallbackServer(options: OAuthCallbackServerOptions) {
return { server, authCode, waitForAuthCode } return { server, authCode, waitForAuthCode }
} }
async function findExistingClientPort(serverUrlHash: string): Promise<number | undefined> {
const clientInfo = await readJsonFile<OAuthClientInformationFull>(serverUrlHash, 'client_info.json', OAuthClientInformationFullSchema)
if (!clientInfo) {
return undefined
}
const localhostRedirectUri = clientInfo.redirect_uris.map((uri) => new URL(uri)).find(({ hostname }) => hostname === 'localhost')
if (!localhostRedirectUri) {
throw new Error('Cannot find localhost callback URI from existing client information')
}
return parseInt(localhostRedirectUri.port)
}
function calculateDefaultPort(serverUrlHash: string): number {
// Convert the first 4 bytes of the serverUrlHash into a port offset
const offset = parseInt(serverUrlHash.substring(0, 4), 16)
// Pick a consistent but random-seeming port from 3335 to 49151
return 3335 + (offset % 45816)
}
/** /**
* Finds an available port on the local machine * Finds an available port on the local machine
* @param preferredPort Optional preferred port to try first * @param preferredPort Optional preferred port to try first
@ -378,11 +409,10 @@ export async function findAvailablePort(preferredPort?: number): Promise<number>
/** /**
* Parses command line arguments for MCP clients and proxies * Parses command line arguments for MCP clients and proxies
* @param args Command line arguments * @param args Command line arguments
* @param defaultPort Default port for the callback server if specified port is unavailable
* @param usage Usage message to show on error * @param usage Usage message to show on error
* @returns A promise that resolves to an object with parsed serverUrl, callbackPort and headers * @returns A promise that resolves to an object with parsed serverUrl, callbackPort and headers
*/ */
export async function parseCommandLineArgs(args: string[], defaultPort: number, usage: string) { export async function parseCommandLineArgs(args: string[], usage: string) {
// Process headers // Process headers
const headers: Record<string, string> = {} const headers: Record<string, string> = {}
let i = 0 let i = 0
@ -432,14 +462,28 @@ export async function parseCommandLineArgs(args: string[], defaultPort: number,
log(usage) log(usage)
process.exit(1) process.exit(1)
} }
const serverUrlHash = getServerUrlHash(serverUrl)
const defaultPort = calculateDefaultPort(serverUrlHash)
// Use the specified port, or find an available one // Use the specified port, or the existing client port or fallback to find an available one
const callbackPort = specifiedPort || (await findAvailablePort(defaultPort)) const [existingClientPort, availablePort] = await Promise.all([findExistingClientPort(serverUrlHash), findAvailablePort(defaultPort)])
let callbackPort: number
if (specifiedPort) { if (specifiedPort) {
log(`Using specified callback port: ${callbackPort}`) if (existingClientPort && specifiedPort !== existingClientPort) {
log(
`Warning! Specified callback port of ${specifiedPort}, which conflicts with existing client registration port ${existingClientPort}. Deleting existing client data to force reregistration.`,
)
await fs.rm(getConfigFilePath(serverUrlHash, 'client_info.json'))
}
log(`Using specified callback port: ${specifiedPort}`)
callbackPort = specifiedPort
} else if (existingClientPort) {
log(`Using existing client port: ${existingClientPort}`)
callbackPort = existingClientPort
} else { } else {
log(`Using automatically selected callback port: ${callbackPort}`) log(`Using automatically selected callback port: ${availablePort}`)
callbackPort = availablePort
} }
if (Object.keys(headers).length > 0) { if (Object.keys(headers).length > 0) {
@ -477,6 +521,11 @@ export function setupSignalHandlers(cleanup: () => Promise<void>) {
// Keep the process alive // Keep the process alive
process.stdin.resume() process.stdin.resume()
process.stdin.on('end', async () => {
log('\nShutting down...')
await cleanup()
process.exit(0)
})
} }
/** /**

View file

@ -135,7 +135,7 @@ to the CA certificate file. If using claude_desktop_config.json, this might look
} }
// Parse command-line arguments and run the proxy // Parse command-line arguments and run the proxy
parseCommandLineArgs(process.argv.slice(2), 3334, 'Usage: npx tsx proxy.ts <https://server-url> [callback-port]') parseCommandLineArgs(process.argv.slice(2), 'Usage: npx tsx proxy.ts <https://server-url> [callback-port]')
.then(({ serverUrl, callbackPort, headers, transportStrategy }) => { .then(({ serverUrl, callbackPort, headers, transportStrategy }) => {
return runProxy(serverUrl, callbackPort, headers, transportStrategy) return runProxy(serverUrl, callbackPort, headers, transportStrategy)
}) })