Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
dd6bd01
Implement Official MCP Server For Documentation Related to this Repos…
H0llyW00dzZ Sep 27, 2025
261b0b8
Refactor Testing
H0llyW00dzZ Sep 27, 2025
02b013e
Reorganize Codebase to avoid conflict
H0llyW00dzZ Sep 27, 2025
8335d0c
Refactor Testing
H0llyW00dzZ Sep 27, 2025
2c0703e
Adds Documentation Template
H0llyW00dzZ Sep 27, 2025
1aa457f
Improve MCP Documentation Template System
H0llyW00dzZ Sep 27, 2025
33692a2
Update README.md
H0llyW00dzZ Sep 27, 2025
16daae8
Update README.md
H0llyW00dzZ Sep 27, 2025
8a911c1
Improve One API Official MCP Documentation Template System
H0llyW00dzZ Sep 27, 2025
470a780
Update README.md
H0llyW00dzZ Sep 27, 2025
6467f63
Update README.md
H0llyW00dzZ Sep 27, 2025
264abdd
Update Go Documentation
H0llyW00dzZ Sep 27, 2025
0617f25
Update Go Documentation
H0llyW00dzZ Sep 27, 2025
b28dc6d
Adds Support Instruction System
H0llyW00dzZ Sep 27, 2025
7aecc2a
Fix Missing
H0llyW00dzZ Sep 27, 2025
50713ea
Update README.md
H0llyW00dzZ Sep 27, 2025
90ea3ae
Increase Testing Coverage
H0llyW00dzZ Sep 27, 2025
3a6604b
Refactor Documentation
H0llyW00dzZ Sep 27, 2025
c097a85
Improve MCP Handler
H0llyW00dzZ Sep 27, 2025
dd27430
Update README.md
H0llyW00dzZ Sep 28, 2025
d53d869
Implement "NewGinStreamableHTTPHandler"
H0llyW00dzZ Sep 28, 2025
abe2e84
Improve API Router
H0llyW00dzZ Sep 28, 2025
be060bf
Adds TestMCPSingletonConcurrency
H0llyW00dzZ Sep 28, 2025
20471c4
Refactor Go Docs
H0llyW00dzZ Sep 28, 2025
a56a6e8
Refactor MCP Endpoint
H0llyW00dzZ Sep 28, 2025
cbb22cd
Improve MCP Documentation Template System
H0llyW00dzZ Sep 28, 2025
9042bfa
Adds documentation resources
H0llyW00dzZ Sep 29, 2025
ab29dc4
Adds Tests addDocumentationResources function
H0llyW00dzZ Sep 29, 2025
27257b9
Update MCP Go SDK to Latest
H0llyW00dzZ Oct 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/jinzhu/copier v0.4.0
github.com/joho/godotenv v1.5.1
github.com/modelcontextprotocol/go-sdk v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkoukk/tiktoken-go v0.1.7
github.com/prometheus/client_golang v1.23.0
Expand Down Expand Up @@ -87,6 +88,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932 // indirect
github.com/google/jsonschema-go v0.3.0 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/googleapis/gax-go/v2 v2.15.0 // indirect
Expand Down Expand Up @@ -122,6 +124,7 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
github.com/xlzd/gotp v0.1.0 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
go.dedis.ch/kyber/v3 v3.1.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.61.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932/go.mod h1:cC6EdPbj/1
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
github.com/google/jsonschema-go v0.3.0/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -355,6 +357,8 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modelcontextprotocol/go-sdk v1.0.0 h1:Z4MSjLi38bTgLrd/LjSmofqRqyBiVKRyQSJgw8q8V74=
github.com/modelcontextprotocol/go-sdk v1.0.0/go.mod h1:nYtYQroQ2KQiM0/SbyEPUWQ6xs4B95gJjEalc9AQyOs=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -461,6 +465,8 @@ github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
github.com/xlzd/gotp v0.1.0 h1:37blvlKCh38s+fkem+fFh7sMnceltoIEBYTVXyoa5Po=
github.com/xlzd/gotp v0.1.0/go.mod h1:ndLJ3JKzi3xLmUProq4LLxCuECL93dG9WASNLpHz8qg=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
217 changes: 217 additions & 0 deletions mcp/backward_magic_documentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package mcp

// Public API Functions (Exported)
// These functions maintain the original public API for external consumers.

// GenerateChatCompletionsDocumentationFromTemplate generates documentation for
// the OpenAI Chat Completions API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Chat Completions API documentation.
func GenerateChatCompletionsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(ChatCompletions, baseURL)
}

// GenerateCompletionsDocumentationFromTemplate generates documentation for
// the OpenAI Completions API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Completions API documentation.
func GenerateCompletionsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Completions, baseURL)
}

// GenerateEmbeddingsDocumentationFromTemplate generates documentation for
// the OpenAI Embeddings API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Embeddings API documentation.
func GenerateEmbeddingsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Embeddings, baseURL)
}

// GenerateImagesDocumentationFromTemplate generates documentation for
// the OpenAI Image Generation API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Image Generation API documentation.
func GenerateImagesDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Images, baseURL)
}

// GenerateAudioTranscriptionsDocumentationFromTemplate generates documentation for
// the OpenAI Audio Transcriptions API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Audio Transcriptions API documentation.
func GenerateAudioTranscriptionsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(AudioTranscriptions, baseURL)
}

// GenerateAudioTranslationsDocumentationFromTemplate generates documentation for
// the OpenAI Audio Translations API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Audio Translations API documentation.
func GenerateAudioTranslationsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(AudioTranslations, baseURL)
}

// GenerateAudioSpeechDocumentationFromTemplate generates documentation for
// the OpenAI Audio Speech API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Audio Speech API documentation.
func GenerateAudioSpeechDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(AudioSpeech, baseURL)
}

// GenerateModerationsDocumentationFromTemplate generates documentation for
// the OpenAI Moderations API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Moderations API documentation.
func GenerateModerationsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Moderations, baseURL)
}

// GenerateModelsListDocumentationFromTemplate generates documentation for
// the Models List API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Models List API documentation.
func GenerateModelsListDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(ModelsList, baseURL)
}

// GenerateClaudeMessagesDocumentationFromTemplate generates documentation for
// the Claude Messages API using the provided base URL.
//
// This function maintains backward compatibility with the original API while
// internally using the new unified documentation generation system.
//
// Parameters:
// - baseURL: The base URL for the API endpoint (e.g., "https://api.example.com")
//
// Returns a string containing the complete Claude Messages API documentation.
func GenerateClaudeMessagesDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(ClaudeMessages, baseURL)
}

// Internal API Functions (Unexported)
// These functions maintain backward compatibility for internal package usage.

// generateChatCompletionsDocumentationFromTemplate is the internal version of
// the Chat Completions documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateChatCompletionsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(ChatCompletions, baseURL)
}

// generateCompletionsDocumentationFromTemplate is the internal version of
// the Completions documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateCompletionsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Completions, baseURL)
}

// generateEmbeddingsDocumentationFromTemplate is the internal version of
// the Embeddings documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateEmbeddingsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Embeddings, baseURL)
}

// generateImagesDocumentationFromTemplate is the internal version of
// the Images documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateImagesDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Images, baseURL)
}

// generateAudioTranscriptionsDocumentationFromTemplate is the internal version of
// the Audio Transcriptions documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateAudioTranscriptionsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(AudioTranscriptions, baseURL)
}

// generateAudioTranslationsDocumentationFromTemplate is the internal version of
// the Audio Translations documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateAudioTranslationsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(AudioTranslations, baseURL)
}

// generateAudioSpeechDocumentationFromTemplate is the internal version of
// the Audio Speech documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateAudioSpeechDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(AudioSpeech, baseURL)
}

// generateModerationsDocumentationFromTemplate is the internal version of
// the Moderations documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateModerationsDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(Moderations, baseURL)
}

// generateModelsListDocumentationFromTemplate is the internal version of
// the Models List documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateModelsListDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(ModelsList, baseURL)
}

// generateClaudeMessagesDocumentationFromTemplate is the internal version of
// the Claude Messages documentation generator, maintaining compatibility with
// existing internal code that uses lowercase function names.
func generateClaudeMessagesDocumentationFromTemplate(baseURL string) string {
return GenerateDocumentation(ClaudeMessages, baseURL)
}
41 changes: 41 additions & 0 deletions mcp/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mcp

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
)

// Benchmark the handler performance
func BenchmarkHandler(b *testing.B) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/mcp", Handler)

for b.Loop() {
req, _ := http.NewRequest("GET", "/mcp", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
}
}

// Benchmark test for server creation
func BenchmarkNewServer(b *testing.B) {
for b.Loop() {
server := NewServer()
_ = server // Avoid unused variable warning
}
}

// Benchmark test for getBaseURL
func BenchmarkGetBaseURL(b *testing.B) {
config.ServerAddress = "https://api.example.com"

for b.Loop() {
url := getBaseURL()
_ = url // Avoid unused variable warning
}
}
9 changes: 9 additions & 0 deletions mcp/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Package mcp provides [Model Context Protocol (MCP)] server implementation with
// template-based documentation generation for One API endpoints.
//
// This package offers a reusable and scalable architecture for generating
// API documentation using Go templates, supporting multiple API types including
// OpenAI-compatible endpoints and Claude messages.
//
// [Model Context Protocol (MCP)]: https://modelcontextprotocol.io/docs/getting-started/intro
package mcp
Loading
Loading