-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsession.go
More file actions
114 lines (88 loc) · 3.01 KB
/
Copy pathsession.go
File metadata and controls
114 lines (88 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Tencent is pleased to support the open source community by making trpc-mcp-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-mcp-go is licensed under the Apache License Version 2.0.
package mcp
import (
"context"
"time"
"trpc.group/trpc-go/trpc-mcp-go/internal/session"
)
// Session defines the session interface.
type Session interface {
// GetID returns the session ID
GetID() string
// GetCreatedAt returns the session creation time
GetCreatedAt() time.Time
// GetLastActivity returns the last activity time
GetLastActivity() time.Time
// UpdateActivity updates the last activity time
UpdateActivity()
// GetData gets session data
GetData(key string) (interface{}, bool)
// SetData sets session data
SetData(key string, value interface{})
}
// sessionManager defines the session manager interface
type sessionManager interface {
// CreateSession creates a new session
createSession() Session
// GetSession gets a session
getSession(id string) (Session, bool)
// getActiveSessions gets all active session IDs
getActiveSessions() []string
// TerminateSession terminates a session
terminateSession(id string) bool
}
// newSession creates a new session
func newSession() Session {
return session.NewSession()
}
// Session manager adapter that implements the sessionManager interface
type sessionManagerAdapter struct {
manager *session.SessionManager
}
// CreateSession creates a new session
func (a *sessionManagerAdapter) createSession() Session {
return a.manager.CreateSession()
}
// GetSession gets a session
func (a *sessionManagerAdapter) getSession(id string) (Session, bool) {
return a.manager.GetSession(id)
}
// getActiveSessions gets all active session IDs
func (a *sessionManagerAdapter) getActiveSessions() []string {
return a.manager.GetActiveSessions()
}
// TerminateSession terminates a session
func (a *sessionManagerAdapter) terminateSession(id string) bool {
return a.manager.TerminateSession(id)
}
// newSessionManager creates a session manager
func newSessionManager(expirySeconds int) sessionManager {
return &sessionManagerAdapter{
manager: session.NewSessionManager(expirySeconds),
}
}
// Session context key
type sessionContextKey struct{}
// Server context key
type serverContextKey struct{}
// setSessionToContext adds a session to the context
func setSessionToContext(ctx context.Context, session Session) context.Context {
return context.WithValue(ctx, sessionContextKey{}, session)
}
// GetSessionFromContext gets a session from the context
func GetSessionFromContext(ctx context.Context) (Session, bool) {
session, ok := ctx.Value(sessionContextKey{}).(Session)
return session, ok
}
// setServerToContext adds a server instance to the context
func setServerToContext(ctx context.Context, server interface{}) context.Context {
return context.WithValue(ctx, serverContextKey{}, server)
}
// GetServerFromContext gets a server instance from the context
func GetServerFromContext(ctx context.Context) interface{} {
return ctx.Value(serverContextKey{})
}