mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-04 00:03:59 +00:00
docs: 项目概览(OVERVIEW.md) + 插件开发指南(PLUGIN_DEV.md) + MCP 适配器插件
- OVERVIEW.md: 非技术用户友好的项目介绍和目标 - PLUGIN_DEV.md: 完整插件开发指南(含三种开发方式、API 参考、最佳实践) - internal/plugins/mcp/: MCP 协议适配器插件(JSON-RPC over stdio/SSE) - client.go: MCP 客户端(ListTools / CallTool) - stdio.go: 子进程 stdin/stdout 传输 - sse.go: HTTP POST 传输 - plugin.go: init() 自注册 + Settings 配置读取 - README.md 更新链接
This commit is contained in:
61
internal/plugins/mcp/sse.go
Normal file
61
internal/plugins/mcp/sse.go
Normal file
@ -0,0 +1,61 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SSETransport 通过 HTTP POST 进行 JSON-RPC 通信(简化版,非流式)
|
||||
type SSETransport struct {
|
||||
url string
|
||||
client *http.Client
|
||||
pending map[int]chan *rpcResponse
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func NewSSETransport(url string) *SSETransport {
|
||||
return &SSETransport{
|
||||
url: url,
|
||||
client: &http.Client{},
|
||||
pending: make(map[int]chan *rpcResponse),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
func (t *SSETransport) Send(req *rpcRequest) (*rpcResponse, error) {
|
||||
data, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal: %w", err)
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequest("POST", t.url, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http request: %w", err)
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := t.client.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http post: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read body: %w", err)
|
||||
}
|
||||
|
||||
var rpcResp rpcResponse
|
||||
if err := json.Unmarshal(body, &rpcResp); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
return &rpcResp, nil
|
||||
}
|
||||
|
||||
func (t *SSETransport) Close() error {
|
||||
close(t.done)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user