mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-21 10:14:26 +08:00
update token last seen time when call mcp initialize api
This commit is contained in:
@@ -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"
|
||||||
@@ -18,12 +20,12 @@ const mcpServerName = "ezBookkeeping-mcp"
|
|||||||
// ModelContextProtocolAPI represents model context protocol api
|
// ModelContextProtocolAPI represents model context protocol api
|
||||||
type ModelContextProtocolAPI struct {
|
type ModelContextProtocolAPI struct {
|
||||||
ApiUsingConfig
|
ApiUsingConfig
|
||||||
transactions *services.TransactionService
|
transactions *services.TransactionService
|
||||||
transactionCategories *services.TransactionCategoryService
|
transactionCategories *services.TransactionCategoryService
|
||||||
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
|
||||||
@@ -32,12 +34,12 @@ var (
|
|||||||
ApiUsingConfig: ApiUsingConfig{
|
ApiUsingConfig: ApiUsingConfig{
|
||||||
container: settings.Container,
|
container: settings.Container,
|
||||||
},
|
},
|
||||||
transactions: services.Transactions,
|
transactions: services.Transactions,
|
||||||
transactionCategories: services.TransactionCategories,
|
transactionCategories: services.TransactionCategories,
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user