micro--go-micro
d2036b880d
* docs: update all four documentation guides and mark Q2 complete - ai-native-services: add WithMCP one-liner, standalone gateway, WebSocket client example, and OpenTelemetry observability section - mcp-security: add OTel distributed tracing, WebSocket authentication (connection-level and per-message), DeniedReason audit field - tool-descriptions: add manual overrides with WithEndpointDocs and export formats section - agent-patterns: add LangChain/LlamaIndex SDK pattern and standalone gateway production pattern with Docker example - Update roadmap: mark Q2 documentation as complete, Q2 at 100% - Update status: reflect all recent completions, shift priorities https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: add agent demo example and blog post Add examples/agent-demo with a multi-service project management app (projects, tasks, team) that demonstrates AI agents interacting with Go Micro services through MCP. Includes seed data and example prompts. Add blog post 4 "Agents Meet Microservices: A Hands-On Demo" walking through the example code and showing cross-service agent workflows. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: enable multiple services in a single binary Remove global state mutations from service and cmd option functions so that configuring one service no longer overwrites another's settings. Key changes: - service/options.go: remove all DefaultXxx global writes from option functions; newOptions() now creates fresh Server, Client, Store, and Cache per service while sharing Registry, Broker, and Transport - cmd/cmd.go: newCmd() uses local copies instead of pointers to package globals; Before() no longer mutates DefaultXxx vars - cmd/options.go: remove global mutations from all option functions - service/service.go: export ServiceImpl type for cross-package use - service/group.go: new Group type for multi-service lifecycle - micro.go: add Start/Stop to Service interface, expose Group and NewGroup convenience function - examples/multi-service: working example with two services https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * docs: highlight multi-service binary support Add multi-service section to README with code example, update features list, add to examples index, and note in status summary. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: unify service API and clean up developer experience - Unified service creation: micro.New("name", opts...) as canonical API - Clean handler registration: service.Handle(handler, opts...) accepts server.HandlerOption args directly, no need to reach through Server() - Unexported serviceImpl: users interact through Service interface only - Service groups use Service interface (not concrete type) - Fixed Stop() to properly propagate BeforeStop/AfterStop errors - Fixed store init: error-level log instead of fatal on init failure - Updated all examples to use consistent patterns - Updated README, getting-started, MCP docs, and guides - Added blog post about the DX cleanup https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc --------- Co-authored-by: Claude <noreply@anthropic.com>
138 行
4.0 KiB
Go
138 行
4.0 KiB
Go
// Package main demonstrates how to document your service handlers for better
|
|
// AI agent integration using endpoint metadata.
|
|
//
|
|
// Services register descriptions with their endpoints, and the MCP gateway
|
|
// reads these descriptions from the registry to generate rich tool descriptions.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"go-micro.dev/v5"
|
|
"go-micro.dev/v5/gateway/mcp"
|
|
"go-micro.dev/v5/server"
|
|
)
|
|
|
|
// User represents a user in the system
|
|
type User struct {
|
|
ID string `json:"id" description:"User's unique identifier (UUID format)"`
|
|
Name string `json:"name" description:"User's full name"`
|
|
Email string `json:"email" description:"User's email address"`
|
|
Age int `json:"age,omitempty" description:"User's age (optional)"`
|
|
}
|
|
|
|
// GetUserRequest is the request for getting a user
|
|
type GetUserRequest struct {
|
|
ID string `json:"id" description:"User ID to retrieve"`
|
|
}
|
|
|
|
// GetUserResponse is the response containing user data
|
|
type GetUserResponse struct {
|
|
User *User `json:"user" description:"The requested user object"`
|
|
}
|
|
|
|
// CreateUserRequest is the request for creating a user
|
|
type CreateUserRequest struct {
|
|
Name string `json:"name" description:"User's full name (required)"`
|
|
Email string `json:"email" description:"User's email address (required)"`
|
|
Age int `json:"age,omitempty" description:"User's age (optional)"`
|
|
}
|
|
|
|
// CreateUserResponse contains the newly created user
|
|
type CreateUserResponse struct {
|
|
User *User `json:"user" description:"The newly created user"`
|
|
}
|
|
|
|
// Users service handles user-related operations
|
|
type Users struct {
|
|
users map[string]*User
|
|
}
|
|
|
|
// GetUser retrieves a user by ID from the database. Returns full profile including email, name, and preferences. If the user doesn't exist, an error is returned.
|
|
//
|
|
// @example {"id": "user-1"}
|
|
func (u *Users) GetUser(ctx context.Context, req *GetUserRequest, rsp *GetUserResponse) error {
|
|
user, exists := u.users[req.ID]
|
|
if !exists {
|
|
return fmt.Errorf("user not found: %s", req.ID)
|
|
}
|
|
|
|
rsp.User = user
|
|
return nil
|
|
}
|
|
|
|
// CreateUser creates a new user in the system. Validates the user data and creates a new profile. Name and email are required fields, while age is optional. Email must be unique across all users.
|
|
//
|
|
// @example {"name": "Alice Smith", "email": "alice@example.com", "age": 30}
|
|
func (u *Users) CreateUser(ctx context.Context, req *CreateUserRequest, rsp *CreateUserResponse) error {
|
|
// Validate input
|
|
if req.Name == "" || req.Email == "" {
|
|
return fmt.Errorf("name and email are required")
|
|
}
|
|
|
|
// Generate ID (simplified for example)
|
|
id := fmt.Sprintf("user-%d", len(u.users)+1)
|
|
|
|
user := &User{
|
|
ID: id,
|
|
Name: req.Name,
|
|
Email: req.Email,
|
|
Age: req.Age,
|
|
}
|
|
|
|
u.users[id] = user
|
|
rsp.User = user
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
// Create service
|
|
service := micro.New("users",
|
|
micro.Address(":9090"),
|
|
// Start MCP gateway alongside the service
|
|
mcp.WithMCP(":3000"),
|
|
)
|
|
|
|
service.Init()
|
|
|
|
// Register handler with pre-populated test data.
|
|
// Documentation is automatically extracted from method comments.
|
|
// Use WithEndpointScopes to declare required auth scopes per endpoint.
|
|
if err := service.Handle(
|
|
&Users{
|
|
users: map[string]*User{
|
|
"user-1": {ID: "user-1", Name: "John Doe", Email: "john@example.com", Age: 25},
|
|
"user-2": {ID: "user-2", Name: "Jane Smith", Email: "jane@example.com", Age: 30},
|
|
},
|
|
},
|
|
server.WithEndpointScopes("Users.GetUser", "users:read"),
|
|
server.WithEndpointScopes("Users.CreateUser", "users:write"),
|
|
); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println("Users service starting...")
|
|
log.Println("Service: users")
|
|
log.Println("Endpoints:")
|
|
log.Println(" - Users.GetUser")
|
|
log.Println(" - Users.CreateUser")
|
|
log.Println("MCP Gateway: http://localhost:3000")
|
|
log.Println("")
|
|
log.Println("Test with:")
|
|
log.Println(" curl http://localhost:3000/mcp/tools")
|
|
log.Println("")
|
|
log.Println("Or add to Claude Code:")
|
|
log.Println(` "users-service": {`)
|
|
log.Println(` "command": "micro",`)
|
|
log.Println(` "args": ["mcp", "serve"]`)
|
|
log.Println(` }`)
|
|
|
|
// Run service
|
|
if err := service.Run(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|