From aca40d8835e0d65a8b1db98a5da07d32e53fcd57 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Wed, 19 Feb 2025 15:57:48 +0100 Subject: [PATCH] Add stdio-sse-proxy dev tool --- devtools/stdio-sse-proxy/README.md | 55 +++ devtools/stdio-sse-proxy/mvnw | 259 +++++++++++++++ devtools/stdio-sse-proxy/mvnw.cmd | 149 +++++++++ devtools/stdio-sse-proxy/pom.xml | 92 +++++ .../mcp/server/proxy/StdioSseProxy.java | 313 ++++++++++++++++++ .../src/main/resources/application.properties | 3 + pom.xml | 3 +- 7 files changed, 873 insertions(+), 1 deletion(-) create mode 100644 devtools/stdio-sse-proxy/README.md create mode 100755 devtools/stdio-sse-proxy/mvnw create mode 100644 devtools/stdio-sse-proxy/mvnw.cmd create mode 100644 devtools/stdio-sse-proxy/pom.xml create mode 100644 devtools/stdio-sse-proxy/src/main/java/io/quarkiverse/mcp/server/proxy/StdioSseProxy.java create mode 100644 devtools/stdio-sse-proxy/src/main/resources/application.properties diff --git a/devtools/stdio-sse-proxy/README.md b/devtools/stdio-sse-proxy/README.md new file mode 100644 index 0000000..a865c86 --- /dev/null +++ b/devtools/stdio-sse-proxy/README.md @@ -0,0 +1,55 @@ +# stdio -> SSE MCP Proxy + +Starts a proxy server between an MCP client using the `stdio` transport and an MCP server using the `HTTP/SSE` transport. +Sends all MCP messages to the target MCP server and writes the MCP responses to the standard output respectively. + +## Packaging and running the application + +The application can be packaged using: + +```shell script +./mvnw clean package +``` + +> [!NOTE] +> You can also build a native executable using `./mvnw clean package -Dnative`. + +This builds a Quarkus uber-jar that can be run directly: + +```shell script +java -jar target/quarkus-mcp-stdio-sse-proxy-1.0.0-SNAPSHOT-runner.jar --help +``` + +Shows the following help message: + +```shell script +Usage: stdio-sse-proxy [-hV] [--[no-]reconnect] -e= [-s=] + [-t=] + -e, --endpoint= + The URI of the target SSE endpoint + -h, --help Show this help message and exit. + --[no-]reconnect If set to true then the proxy attempts to reconnect + if a message endpoint returns http status 400 + -s, --sleep= The sleep time in milliseconds; used when + processing the stdin queue + -t, --timeout= The timeout in seconds; used when connecting to the + SSE endpoint and to obtain the message endpoint + -V, --version Print version information and exit. +``` + +In order to run a proxy server for a `quarkus-mcp-server` application running in the dev mode: + +```shell script +java -jar target/quarkus-mcp-stdio-sse-proxy-1.0.0-SNAPSHOT-runner.jar -e http://localhost:8080/mcp/sse +``` + +> [!IMPORTANT] +> The target SSE endpoint is mandatory. + +## JBang + +You can also run the proxy as a JBang script: + +```shell script +jbang --quiet src/main/java/io/quarkiverse/mcp/server/proxy/StdioSseProxy.java -h +``` \ No newline at end of file diff --git a/devtools/stdio-sse-proxy/mvnw b/devtools/stdio-sse-proxy/mvnw new file mode 100755 index 0000000..19529dd --- /dev/null +++ b/devtools/stdio-sse-proxy/mvnw @@ -0,0 +1,259 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.2 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/devtools/stdio-sse-proxy/mvnw.cmd b/devtools/stdio-sse-proxy/mvnw.cmd new file mode 100644 index 0000000..249bdf3 --- /dev/null +++ b/devtools/stdio-sse-proxy/mvnw.cmd @@ -0,0 +1,149 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' +$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" +if ($env:MAVEN_USER_HOME) { + $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" +} +$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/devtools/stdio-sse-proxy/pom.xml b/devtools/stdio-sse-proxy/pom.xml new file mode 100644 index 0000000..7f12914 --- /dev/null +++ b/devtools/stdio-sse-proxy/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + io.quarkiverse.mcp + quarkus-mcp-server-parent + 999-SNAPSHOT + ../../pom.xml + + quarkus-mcp-stdio-sse-proxy + Quarkus MCP Server - stdio/SSE Proxy + + + + io.quarkus + quarkus-picocli + + + io.quarkus + quarkus-junit5 + test + + + + + + + io.quarkus + quarkus-maven-plugin + true + + + + build + generate-code + generate-code-tests + native-image-agent + + + + + + maven-compiler-plugin + + true + + + + maven-surefire-plugin + + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + + native + + + native + + + + false + true + + + + diff --git a/devtools/stdio-sse-proxy/src/main/java/io/quarkiverse/mcp/server/proxy/StdioSseProxy.java b/devtools/stdio-sse-proxy/src/main/java/io/quarkiverse/mcp/server/proxy/StdioSseProxy.java new file mode 100644 index 0000000..5bcc9b4 --- /dev/null +++ b/devtools/stdio-sse-proxy/src/main/java/io/quarkiverse/mcp/server/proxy/StdioSseProxy.java @@ -0,0 +1,313 @@ +//usr/bin/env jbang "$0" "$@" ; exit $? +//DEPS io.quarkus.platform:quarkus-bom:3.18.3@pom +//DEPS io.quarkus:quarkus-picocli +//Q:CONFIG quarkus.log.console.stderr=true +package io.quarkiverse.mcp.server.proxy; + +import java.io.BufferedReader; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ConnectException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpClient.Version; +import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublishers; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow; +import java.util.concurrent.Phaser; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.jboss.logging.Logger; + +import io.quarkus.runtime.Quarkus; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + +/** + * Starts a proxy server between an MCP client using the {@code stdio} transport and an MCP server using the {@code HTTP/SSE} + * transport. Sends all MCP messages to the target MCP server and writes the MCP responses to the standard output respectively. + */ +@Command(name = "stdio-sse-proxy", mixinStandardHelpOptions = true) +public class StdioSseProxy implements Runnable { + + private static final Logger LOG = Logger.getLogger(StdioSseProxy.class); + + @Option(names = { "-e", "--endpoint" }, required = true, description = "The URI of the target SSE endpoint") + URI sseEndpoint; + + @Option(names = { "-t", + "--timeout" }, defaultValue = "10", description = "The timeout in seconds; used when connecting to the SSE endpoint and to obtain the message endpoint") + int timeout; + + @Option(names = { "-s", + "--sleep" }, defaultValue = "60", description = "The sleep time in milliseconds; used when processing the stdin queue") + int sleep; + + @Option(names = { + "--reconnect" }, negatable = true, defaultValue = "true", description = "If set to true then the proxy attempts to reconnect if a message endpoint returns http status 400") + boolean reconnect; + + @Override + public void run() { + LOG.infof("Stdio -> SSE [sse: %s, timeout: %s, reconnect: %s, sleep: %s]", sseEndpoint, timeout, reconnect, sleep); + + InputStream in = System.in; + PrintStream out = System.out; + System.setOut(new PrintStream(OutputStream.nullOutputStream())); + Queue inQueue = new ConcurrentLinkedQueue<>(); + + ExecutorService executor = Executors.newFixedThreadPool(2); + HttpClient client = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(timeout)) + .build(); + + AtomicReference messageEndpoint = new AtomicReference<>(); + EndpointPhaser endpointReceived = new EndpointPhaser(); + + SseClient sseClient = new SseClient(sseEndpoint) { + @Override + protected void process(SseEvent event) { + if ("endpoint".equals(event.name())) { + String endpoint = event.data().strip(); + LOG.infof("Message endpoint received: %s", endpoint); + messageEndpoint.set(sseEndpoint.resolve(endpoint)); + endpointReceived.countDown(); + } else { + out.println(event.data()); + } + } + }; + sseClient.connect(client); + + executor.submit(new Runnable() { + @Override + public void run() { + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { + while (true) { + String line = reader.readLine(); + if (line == null) { + LOG.debug("EOF received, exiting"); + Quarkus.asyncExit(0); + return; + } + if (line.isBlank()) { + continue; + } + LOG.debugf("Line added to queue:\n%s", line); + inQueue.offer(line); + } + } catch (IOException e) { + LOG.errorf(e, "Error reading stdio"); + } + } + }); + + executor.submit(new Runnable() { + + @Override + public void run() { + try { + while (true) { + try { + endpointReceived.await(timeout, TimeUnit.SECONDS); + } catch (TimeoutException e) { + LOG.errorf(e, "Message endpoint not received in %s seconds", timeout); + } + String line = inQueue.poll(); + if (line != null && !line.isBlank()) { + try { + sendData(sseClient, endpointReceived, client, messageEndpoint, line, false); + } catch (IOException e) { + LOG.errorf(e, "Unable to send POST request to %s", messageEndpoint.get()); + } + } + TimeUnit.MILLISECONDS.sleep(sleep); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + }); + + Quarkus.waitForExit(); + } + + private void sendData(SseClient sseClient, EndpointPhaser endpointReceived, HttpClient httpClient, + AtomicReference messageEndpoint, String data, + boolean reconnectAttempt) + throws IOException, InterruptedException { + LOG.debugf("%s data to SSE:\n%s", reconnectAttempt ? "Resending" : "Sending", data); + HttpRequest request = HttpRequest.newBuilder() + .uri(messageEndpoint.get()) + .version(Version.HTTP_1_1) + .POST(BodyPublishers.ofString(data)) + .build(); + HttpResponse response = httpClient.send(request, BodyHandlers.discarding()); + if (response.statusCode() == 400 && reconnect && !reconnectAttempt) { + // The connection was very likely closed + // Let's reconnect the SSE client + LOG.infof("Message endpoint %s not found - reconnecting SSE client..", messageEndpoint.get()); + endpointReceived.reset(); + sseClient.connect(httpClient); + try { + endpointReceived.await(timeout, TimeUnit.SECONDS); + } catch (TimeoutException e) { + LOG.errorf(e, "Message endpoint not received in %s seconds", timeout); + return; + } + sendData(sseClient, endpointReceived, httpClient, messageEndpoint, data, true); + } else if (response.statusCode() != 200) { + LOG.errorf("Received erroneous status code: %s", response.statusCode()); + } + } + + public abstract class SseClient { + + private static final Logger LOG = Logger.getLogger(SseClient.class); + + private final URI sseUri; + + public SseClient(URI uri) { + this.sseUri = uri; + } + + protected abstract void process(SseEvent event); + + public void connect(HttpClient client) { + if (client == null) { + client = HttpClient.newHttpClient(); + } + HttpRequest request = HttpRequest.newBuilder() + .uri(sseUri) + .version(Version.HTTP_1_1) + .header("Accept", "text/event-stream") + .GET() + .build(); + + client.sendAsync(request, BodyHandlers.fromLineSubscriber(new SseEventSubscriber())) + .exceptionally(e -> { + if (e instanceof CompletionException) { + e = e.getCause(); + } + if (e instanceof ConnectException ce) { + LOG.errorf(ce.getCause(), "Connection failed: %s", sseEndpoint); + Quarkus.asyncExit(0); + } else { + Throwable root = getRootCause(e); + if (!(root instanceof EOFException)) { + LOG.error(e); + } + } + return null; + }); + } + + class SseEventSubscriber implements Flow.Subscriber { + + private Flow.Subscription subscription; + + private String event = "message"; + private StringBuilder dataBuffer = new StringBuilder(); + + @Override + public void onSubscribe(Flow.Subscription subscription) { + this.subscription = subscription; + subscription.request(1); + LOG.infof("Connected to SSE stream: %s", sseUri); + } + + @Override + public void onNext(String line) { + if (line.startsWith(":")) { + // Skip comments + } else if (line.isBlank()) { + // Flush + process(new SseEvent(event, dataBuffer.toString())); + event = "message"; + dataBuffer = new StringBuilder(); + } else if (line.contains(":")) { + LOG.debugf("Received next line:\n%s", line); + int colon = line.indexOf(":"); + String field = line.substring(0, colon).strip(); + String value = line.substring(colon + 1).strip(); + handleField(field, value); + } else { + // The whole line is the field name + handleField(line, ""); + } + subscription.request(1); + } + + @Override + public void onError(Throwable t) { + Throwable root = getRootCause(t); + if (!(root instanceof EOFException)) { + LOG.errorf(t, "Error in SSE stream"); + } + } + + @Override + public void onComplete() { + LOG.info("SSE connection closed"); + } + + private void handleField(String field, String value) { + switch (field) { + case "event" -> event = value; + case "data" -> dataBuffer.append(value).append("\n"); + } + } + } + + public record SseEvent(String name, String data) { + } + + private static Throwable getRootCause(Throwable exception) { + final List chain = new ArrayList<>(); + Throwable curr = exception; + while (curr != null && !chain.contains(curr)) { + chain.add(curr); + curr = curr.getCause(); + } + return chain.isEmpty() ? null : chain.get(chain.size() - 1); + } + } + + class EndpointPhaser { + + private final Phaser phaser = new Phaser(1); + + private final AtomicInteger phase = new AtomicInteger(0); + + public void countDown() { + phaser.arrive(); + } + + public void await(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException { + phaser.awaitAdvanceInterruptibly(phase.get(), timeout, unit); + } + + public void reset() { + phase.incrementAndGet(); + } + } + +} diff --git a/devtools/stdio-sse-proxy/src/main/resources/application.properties b/devtools/stdio-sse-proxy/src/main/resources/application.properties new file mode 100644 index 0000000..6f274e9 --- /dev/null +++ b/devtools/stdio-sse-proxy/src/main/resources/application.properties @@ -0,0 +1,3 @@ +quarkus.package.jar.type=uber-jar +quarkus.log.console.stderr=true +#quarkus.log.category."io.quarkiverse.mcp.server.proxy".level=DEBUG \ No newline at end of file diff --git a/pom.xml b/pom.xml index a48f40c..9173146 100644 --- a/pom.xml +++ b/pom.xml @@ -18,13 +18,14 @@ transports/sse transports/stdio test-utils + devtools/stdio-sse-proxy scm:git:git@github.com:quarkiverse/quarkus-mcp-server.git scm:git:git@github.com:quarkiverse/quarkus-mcp-server.git https://github.com/quarkiverse/quarkus-mcp-server - HEAD + HEAD