update token last seen time when call mcp initialize api

This commit is contained in:
MaysWind
2025-07-07 22:38:50 +08:00
parent 07477eb5f8
commit 773f808a35
+27 -4
View File
@@ -9,8 +9,10 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/errs" "github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/mcp" "github.com/mayswind/ezbookkeeping/pkg/mcp"
"github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/services" "github.com/mayswind/ezbookkeeping/pkg/services"
"github.com/mayswind/ezbookkeeping/pkg/settings" "github.com/mayswind/ezbookkeeping/pkg/settings"
"github.com/mayswind/ezbookkeeping/pkg/utils"
) )
const mcpServerName = "ezBookkeeping-mcp" const mcpServerName = "ezBookkeeping-mcp"
@@ -23,7 +25,7 @@ type ModelContextProtocolAPI struct {
transactionTags *services.TransactionTagService transactionTags *services.TransactionTagService
accounts *services.AccountService accounts *services.AccountService
users *services.UserService users *services.UserService
userCustomExchangeRates *services.UserCustomExchangeRatesService tokens *services.TokenService
} }
// Initialize a model context protocol api singleton instance // Initialize a model context protocol api singleton instance
@@ -37,7 +39,7 @@ var (
transactionTags: services.TransactionTags, transactionTags: services.TransactionTags,
accounts: services.Accounts, accounts: services.Accounts,
users: services.Users, users: services.Users,
userCustomExchangeRates: services.UserCustomExchangeRates, tokens: services.Tokens,
} }
) )
@@ -65,6 +67,27 @@ func (a *ModelContextProtocolAPI) InitializeHandler(c *core.WebContext, jsonRPCR
return nil, errs.ErrNotPermittedToPerformThisAction return nil, errs.ErrNotPermittedToPerformThisAction
} }
tokenClaims := c.GetTokenClaims()
userTokenId, err := utils.StringToInt64(tokenClaims.UserTokenId)
if err != nil {
log.Warnf(c, "[model_context_protocols.InitializeHandler] parse user token id failed, because %s", err.Error())
} else {
tokenRecord := &models.TokenRecord{
Uid: tokenClaims.Uid,
UserTokenId: userTokenId,
CreatedUnixTime: tokenClaims.IssuedAt,
}
tokenId := a.tokens.GenerateTokenId(tokenRecord)
err = a.tokens.UpdateTokenLastSeen(c, tokenRecord)
if err != nil {
log.Warnf(c, "[model_context_protocols.InitializeHandler] failed to update last seen of token \"id:%s\" for user \"uid:%d\", because %s", tokenId, uid, err.Error())
}
}
protocolVersion := mcp.MCPProtocolVersion(initRequest.ProtocolVersion) protocolVersion := mcp.MCPProtocolVersion(initRequest.ProtocolVersion)
_, exists := mcp.SupportedMCPVersion[protocolVersion] _, exists := mcp.SupportedMCPVersion[protocolVersion]
@@ -218,7 +241,7 @@ func (a *ModelContextProtocolAPI) GetTransactionService() *services.TransactionS
return a.transactions return a.transactions
} }
// GetUserCustomExchangeRatesService implements the MCPAvailableServices interface // GetTransactionCategoryService implements the MCPAvailableServices interface
func (a *ModelContextProtocolAPI) GetTransactionCategoryService() *services.TransactionCategoryService { func (a *ModelContextProtocolAPI) GetTransactionCategoryService() *services.TransactionCategoryService {
return a.transactionCategories return a.transactionCategories
} }
@@ -233,7 +256,7 @@ func (a *ModelContextProtocolAPI) GetAccountService() *services.AccountService {
return a.accounts return a.accounts
} }
// GetUserCustomExchangeRatesService implements the MCPAvailableServices interface // GetUserService implements the MCPAvailableServices interface
func (a *ModelContextProtocolAPI) GetUserService() *services.UserService { func (a *ModelContextProtocolAPI) GetUserService() *services.UserService {
return a.users return a.users
} }