(Just add two lines of code to integrate the library!)
Gin-MCP offers a zero-configuration approach. Just plug it into your Gin application, and it automatically discovers your routes and infers basic schemas, exposing them as MCP tools instantly.
Gin-MCP: A Seamless Integration for MCP Features
Gin-MCP is an opinionated, zero-configuration library designed to effortlessly enable Model Context Protocol (MCP) features for any existing Gin API with just a single line of code. This powerful tool automatically exposes Gin endpoints and makes them accessible to compatible MCP clients like Cursor. With a focus on minimal setup and maximum productivity, Gin-MCP allows developers to concentrate on building features instead of being bogged down with configuration tasks.
Key Features
- Effortless Integration: Simplifies the connection between Gin APIs and MCP clients without the need for extensive boilerplate code.
- Zero Configuration (by default): Instantly get started as Gin-MCP automatically discovers routes and infers schemas.
- Boosted Developer Productivity: Focus more on delivering functionalities and less on setting up tools.
- Flexibility: Default zero-configuration can be customized to tailor schemas and endpoint exposure as desired.
- Compatibility with Existing APIs: Utilize the library without modifying existing Gin API code.
Demo
Functional Highlights
- Automatic Discovery: Smartly identifies all registered Gin routes.
- Schema Inference: Generates MCP tool schemas automatically based on route parameters and request/response types when possible.
- Direct Gin Integration: Integrates the MCP server directly into the existing
gin.Engine
. - Parameter Preservation: Reflects Gin route parameters accurately in generated MCP tools.
- Customizable Schemas: Allows manual registration of schema definitions for specific routes using
RegisterSchema
for precision control. - Selective Exposure: Control which endpoints are released based on operation IDs or tags.
- Flexible Deployment: Choose to mount the MCP server within the same Gin application or deploy as a separate service.
Basic Usage
Hide your MCP tools behind a simple setup:
package main
import (
"net/http"
server "github.com/ckanthony/gin-mcp/"
"github.com/gin-gonic/gin"
)
func main() {
// Create a Gin engine
r := gin.Default()
// Define API routes
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "pong"})
})
// Set up MCP server
mcp := server.New(r, &server.Config{
Name: "My Simple API",
Description: "An example API automatically exposed via MCP.",
BaseURL: "http://localhost:8080",
})
// Mount the MCP server endpoint
mcp.Mount("/mcp") // Clients connect here
// Start the Gin server
r.Run(":8080")
}
Gin-MCP automatically generates tools for the defined routes, now available at http://localhost:8080/mcp
.
Advanced Usage
For instances where greater control is required, Gin-MCP allows for customization beyond automatic configuration.
Fine-Grained Schema Control
With RegisterSchema
, explicitly define schemas for query parameters or request bodies of specific routes, addressing complex structs or unique requirements:
mcp.RegisterSchema("GET", "/products", ListProductsParams{}, nil)
mcp.RegisterSchema("POST", "/products", nil, CreateProductRequest{})
Filtering Exposed Endpoints
Control which Gin endpoints become accessible as MCP tools:
mcp := server.New(r, &server.Config{
// ...other config...
IncludeOperations: []string{"getUser", "listUsers"},
})
Connecting an MCP Client
To connect a client like Cursor to your running Gin application:
- Start the application.
- In the MCP client settings, enter the URL to the MCP server endpoint (e.g.,
http://localhost:8080/mcp
). - The client will connect and discover available tools automatically.
For a deeper dive into Gin-MCP, various examples showcasing its features can be found in the examples directory.
Contributions are welcome, and issues or pull requests can be submitted for enhancements or bug fixes.
No comments yet.
Sign in to be the first to comment.