format
This commit is contained in:
parent
65e55c3823
commit
d332273355
8 changed files with 21 additions and 29 deletions
1
.prettierignore
Normal file
1
.prettierignore
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pnpm-lock.yaml
|
|
@ -11,10 +11,7 @@ E.g: Claude Desktop or Windsurf
|
||||||
"mcpServers": {
|
"mcpServers": {
|
||||||
"remote-example": {
|
"remote-example": {
|
||||||
"command": "npx",
|
"command": "npx",
|
||||||
"args": [
|
"args": ["mcp-remote", "https://remote.mcp.server/sse"]
|
||||||
"mcp-remote",
|
|
||||||
"https://remote.mcp.server/sse"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,4 +20,3 @@ E.g: Claude Desktop or Windsurf
|
||||||
Cursor:
|
Cursor:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsup"
|
"build": "tsup",
|
||||||
|
"check": "prettier --check . && tsc"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.21.2",
|
"express": "^4.21.2",
|
||||||
|
|
|
@ -11,13 +11,8 @@
|
||||||
|
|
||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
||||||
import {
|
import { NodeOAuthClientProvider, setupOAuthCallbackServer, parseCommandLineArgs, setupSignalHandlers } from './shared.js'
|
||||||
NodeOAuthClientProvider,
|
import { connectToRemoteServer, mcpProxy } from '../lib/utils.js'
|
||||||
setupOAuthCallbackServer,
|
|
||||||
parseCommandLineArgs,
|
|
||||||
setupSignalHandlers,
|
|
||||||
} from './shared.js'
|
|
||||||
import {connectToRemoteServer, mcpProxy} from "../lib/utils.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function to run the proxy
|
* Main function to run the proxy
|
||||||
|
|
|
@ -10,7 +10,7 @@ import path from 'path'
|
||||||
import os from 'os'
|
import os from 'os'
|
||||||
import crypto from 'crypto'
|
import crypto from 'crypto'
|
||||||
import net from 'net'
|
import net from 'net'
|
||||||
import {OAuthClientProvider} from '@modelcontextprotocol/sdk/client/auth.js'
|
import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js'
|
||||||
import {
|
import {
|
||||||
OAuthClientInformation,
|
OAuthClientInformation,
|
||||||
OAuthClientInformationFull,
|
OAuthClientInformationFull,
|
||||||
|
@ -18,7 +18,7 @@ import {
|
||||||
OAuthTokens,
|
OAuthTokens,
|
||||||
OAuthTokensSchema,
|
OAuthTokensSchema,
|
||||||
} from '@modelcontextprotocol/sdk/shared/auth.js'
|
} from '@modelcontextprotocol/sdk/shared/auth.js'
|
||||||
import {OAuthCallbackServerOptions, OAuthProviderOptions} from "../lib/types.js";
|
import { OAuthCallbackServerOptions, OAuthProviderOptions } from '../lib/types.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the OAuthClientProvider interface for Node.js environments.
|
* Implements the OAuthClientProvider interface for Node.js environments.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {EventEmitter} from "events";
|
import { EventEmitter } from 'events'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options for creating an OAuth client provider
|
* Options for creating an OAuth client provider
|
||||||
|
@ -28,4 +28,4 @@ export interface OAuthCallbackServerOptions {
|
||||||
path: string
|
path: string
|
||||||
/** Event emitter to signal when auth code is received */
|
/** Event emitter to signal when auth code is received */
|
||||||
events: EventEmitter
|
events: EventEmitter
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,23 @@
|
||||||
import { OAuthClientProvider, UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
|
import { OAuthClientProvider, UnauthorizedError } from '@modelcontextprotocol/sdk/client/auth.js'
|
||||||
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
||||||
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a bidirectional proxy between two transports
|
* Creates a bidirectional proxy between two transports
|
||||||
* @param params The transport connections to proxy between
|
* @param params The transport connections to proxy between
|
||||||
*/
|
*/
|
||||||
export function mcpProxy({transportToClient, transportToServer}: {
|
export function mcpProxy({ transportToClient, transportToServer }: { transportToClient: Transport; transportToServer: Transport }) {
|
||||||
transportToClient: Transport;
|
|
||||||
transportToServer: Transport
|
|
||||||
}) {
|
|
||||||
let transportToClientClosed = false
|
let transportToClientClosed = false
|
||||||
let transportToServerClosed = false
|
let transportToServerClosed = false
|
||||||
|
|
||||||
transportToClient.onmessage = (message) => {
|
transportToClient.onmessage = (message) => {
|
||||||
|
// @ts-expect-error TODO
|
||||||
console.error('[Local→Remote]', message.method || message.id)
|
console.error('[Local→Remote]', message.method || message.id)
|
||||||
transportToServer.send(message).catch(onServerError)
|
transportToServer.send(message).catch(onServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
transportToServer.onmessage = (message) => {
|
transportToServer.onmessage = (message) => {
|
||||||
|
// @ts-expect-error TODO: fix this type
|
||||||
console.error('[Remote→Local]', message.method || message.id)
|
console.error('[Remote→Local]', message.method || message.id)
|
||||||
transportToClient.send(message).catch(onClientError)
|
transportToClient.send(message).catch(onClientError)
|
||||||
}
|
}
|
||||||
|
@ -66,7 +65,7 @@ export async function connectToRemoteServer(
|
||||||
): Promise<SSEClientTransport> {
|
): Promise<SSEClientTransport> {
|
||||||
console.error('Connecting to remote server:', serverUrl)
|
console.error('Connecting to remote server:', serverUrl)
|
||||||
const url = new URL(serverUrl)
|
const url = new URL(serverUrl)
|
||||||
const transport = new SSEClientTransport(url, {authProvider})
|
const transport = new SSEClientTransport(url, { authProvider })
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await transport.start()
|
await transport.start()
|
||||||
|
@ -84,7 +83,7 @@ export async function connectToRemoteServer(
|
||||||
await transport.finishAuth(code)
|
await transport.finishAuth(code)
|
||||||
|
|
||||||
// Create a new transport after auth
|
// Create a new transport after auth
|
||||||
const newTransport = new SSEClientTransport(url, {authProvider})
|
const newTransport = new SSEClientTransport(url, { authProvider })
|
||||||
await newTransport.start()
|
await newTransport.start()
|
||||||
console.error('Connected to remote server after authentication')
|
console.error('Connected to remote server after authentication')
|
||||||
return newTransport
|
return newTransport
|
||||||
|
@ -97,4 +96,4 @@ export async function connectToRemoteServer(
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -534,7 +534,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
metadataRef.current = await discoverOAuthMetadata(url)
|
metadataRef.current = await discoverOAuthMetadata(url)
|
||||||
addLog('debug', `OAuth metadata: ${metadataRef.current ? 'Found' : 'Not available'}`)
|
addLog('debug', `OAuth metadata: ${metadataRef.current ? 'Found' : 'Not available'}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If metadata is found, start auth flow
|
// If metadata is found, start auth flow
|
||||||
if (metadataRef.current) {
|
if (metadataRef.current) {
|
||||||
setState('authenticating')
|
setState('authenticating')
|
||||||
|
@ -599,7 +599,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
}
|
}
|
||||||
} catch (connectErr) {
|
} catch (connectErr) {
|
||||||
addLog('error', `Client connect error: ${connectErr instanceof Error ? connectErr.message : String(connectErr)}`)
|
addLog('error', `Client connect error: ${connectErr instanceof Error ? connectErr.message : String(connectErr)}`)
|
||||||
|
|
||||||
if (connectErr instanceof Error && connectErr.message.includes('Unauthorized')) {
|
if (connectErr instanceof Error && connectErr.message.includes('Unauthorized')) {
|
||||||
// Only discover OAuth and authenticate if we get a 401
|
// Only discover OAuth and authenticate if we get a 401
|
||||||
await discoverOAuthAndAuthenticate(connectErr)
|
await discoverOAuthAndAuthenticate(connectErr)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue