1
1
package proxy
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"net/http"
7
+ "time"
6
8
7
9
"github.com/chatmcp/mcprouter/service/jsonrpc"
8
10
"github.com/chatmcp/mcprouter/service/mcpclient"
@@ -32,12 +34,37 @@ func Messages(c echo.Context) error {
32
34
return ctx .JSONRPCError (jsonrpc .ErrorParseError , nil )
33
35
}
34
36
37
+ proxyInfo := session .ProxyInfo ()
35
38
sseKey := session .Key ()
36
39
40
+ proxyInfo .JSONRPCVersion = request .JSONRPC
41
+ proxyInfo .RequestMethod = request .Method
42
+ proxyInfo .RequestTime = time .Now ()
43
+ proxyInfo .RequestParams = request .Params
44
+
45
+ if request .ID != nil {
46
+ proxyInfo .RequestID = request .ID
47
+ }
48
+
49
+ if request .Method == "initialize" {
50
+ paramsB , _ := json .Marshal (request .Params )
51
+ params := & jsonrpc.InitializeParams {}
52
+ if err := json .Unmarshal (paramsB , params ); err != nil {
53
+ return ctx .JSONRPCError (jsonrpc .ErrorParseError , nil )
54
+ }
55
+
56
+ proxyInfo .ClientName = params .ClientInfo .Name
57
+ proxyInfo .ClientVersion = params .ClientInfo .Version
58
+ proxyInfo .ProtocolVersion = params .ProtocolVersion
59
+
60
+ session .SetProxyInfo (proxyInfo )
61
+ ctx .StoreSession (sessionID , session )
62
+ }
63
+
37
64
client := ctx .GetClient (sseKey )
38
65
39
66
if client == nil {
40
- command := session . Command ()
67
+ command := proxyInfo . ServerCommand
41
68
_client , err := mcpclient .NewStdioClient (command )
42
69
if err != nil {
43
70
fmt .Printf ("connect to mcp server failed: %v\n " , err )
@@ -50,7 +77,6 @@ func Messages(c echo.Context) error {
50
77
}
51
78
52
79
ctx .StoreClient (sseKey , _client )
53
- ctx .StoreSession (sessionID , session )
54
80
55
81
client = _client
56
82
@@ -67,13 +93,36 @@ func Messages(c echo.Context) error {
67
93
response , err := client .ForwardMessage (request )
68
94
if err != nil {
69
95
fmt .Printf ("forward message failed: %v\n " , err )
96
+ session .Close ()
97
+ ctx .DeleteClient (sseKey )
70
98
return ctx .JSONRPCError (jsonrpc .ErrorProxyError , request .ID )
71
99
}
72
100
73
101
if response != nil {
102
+ if request .Method == "initialize" && response .Result != nil {
103
+ resultB , _ := json .Marshal (response .Result )
104
+ result := & jsonrpc.InitializeResult {}
105
+ if err := json .Unmarshal (resultB , result ); err != nil {
106
+ fmt .Printf ("unmarshal initialize result failed: %v\n " , err )
107
+ return ctx .JSONRPCError (jsonrpc .ErrorParseError , request .ID )
108
+ }
109
+
110
+ proxyInfo .ServerName = result .ServerInfo .Name
111
+ proxyInfo .ServerVersion = result .ServerInfo .Version
112
+
113
+ session .SetProxyInfo (proxyInfo )
114
+ ctx .StoreSession (sessionID , session )
115
+ }
116
+
117
+ // not notification message, send sse message
74
118
session .SendMessage (response .String ())
75
119
}
76
120
77
- // notification message
121
+ proxyInfo .ResponseTime = time .Now ()
122
+ proxyInfo .ResponseDuration = time .Since (proxyInfo .RequestTime )
123
+ proxyInfoB , _ := json .Marshal (proxyInfo )
124
+
125
+ fmt .Printf ("proxyInfo: %s, cost: %f\n " , string (proxyInfoB ), proxyInfo .ResponseDuration .Seconds ())
126
+
78
127
return ctx .JSONRPCResponse (response )
79
128
}
0 commit comments