add mcp (Model Context Protocol) support

This commit is contained in:
MaysWind
2025-07-06 03:02:19 +08:00
parent 620ccf317f
commit 8dce0f2d6a
32 changed files with 1379 additions and 20 deletions
+41
View File
@@ -60,6 +60,47 @@ func PrintJsonErrorResult(c *core.WebContext, err *errs.Error) {
c.AbortWithStatusJSON(err.HttpStatusCode, result)
}
// PrintJSONRPCSuccessResult writes success response in JSON-RPC format to current http context
func PrintJSONRPCSuccessResult(c *core.WebContext, jsonRPCRequest *core.JSONRPCRequest, result any) {
c.JSON(http.StatusOK, core.NewJSONRPCResponse(jsonRPCRequest.ID, result))
}
// PrintJSONRPCErrorResult writes error response in JSON-RPC format to current http context
func PrintJSONRPCErrorResult(c *core.WebContext, jsonRPCRequest *core.JSONRPCRequest, err *errs.Error) {
c.SetResponseError(err)
errorMessage := err.Error()
if err.Code() == errs.ErrIncompleteOrIncorrectSubmission.Code() && len(err.BaseError) > 0 {
validationErrors, ok := err.BaseError[0].(validator.ValidationErrors)
if ok {
for _, err := range validationErrors {
errorMessage = getValidationErrorText(err)
break
}
}
}
var id any
if jsonRPCRequest != nil {
id = jsonRPCRequest.ID
}
jsonRPCError := core.JSONRPCInternalError
if err.Code() == errs.ErrIncompleteOrIncorrectSubmission.Code() {
jsonRPCError = core.JSONRPCParseError
} else if err.Code() == errs.ErrApiNotFound.Code() {
jsonRPCError = core.JSONRPCMethodNotFoundError
} else if err.Code() == errs.ErrParameterInvalid.Code() {
jsonRPCError = core.JSONRPCInvalidParamsError
}
c.AbortWithStatusJSON(err.HttpStatusCode, core.NewJSONRPCErrorResponseWithCause(id, jsonRPCError, errorMessage))
}
// PrintDataErrorResult writes error response in custom content type to current http context
func PrintDataErrorResult(c *core.WebContext, contentType string, err *errs.Error) {
c.SetResponseError(err)
+32 -9
View File
@@ -9,15 +9,16 @@ import (
)
const (
longDateFormat = "2006-01-02"
longDateTimeFormat = "2006-01-02 15:04:05"
longDateTimeWithTimezoneFormat = "2006-01-02 15:04:05Z07:00"
longDateTimeWithTimezoneFormat2 = "2006-01-02 15:04:05 Z0700"
longDateTimeWithoutSecondFormat = "2006-01-02 15:04"
shortDateTimeFormat = "2006-1-2 15:4:5"
yearMonthDateTimeFormat = "2006-01"
westernmostTimezoneUtcOffset = -720 // Etc/GMT+12 (UTC-12:00)
easternmostTimezoneUtcOffset = 840 // Pacific/Kiritimati (UTC+14:00)
longDateFormat = "2006-01-02"
longDateTimeFormat = "2006-01-02 15:04:05"
longDateTimeWithTimezoneFormat = "2006-01-02 15:04:05Z07:00"
longDateTimeWithTimezoneFormat2 = "2006-01-02 15:04:05 Z0700"
longDateTimeWithTimezoneRFC3389Format = "2006-01-02T15:04:05Z07:00"
longDateTimeWithoutSecondFormat = "2006-01-02 15:04"
shortDateTimeFormat = "2006-1-2 15:4:5"
yearMonthDateTimeFormat = "2006-01"
westernmostTimezoneUtcOffset = -720 // Etc/GMT+12 (UTC-12:00)
easternmostTimezoneUtcOffset = 840 // Pacific/Kiritimati (UTC+14:00)
)
// ParseNumericYearMonth returns numeric year and month from textual content
@@ -65,6 +66,28 @@ func FormatUnixTimeToLongDateTime(unixTime int64, timezone *time.Location) strin
return t.Format(longDateTimeFormat)
}
// FormatUnixTimeToLongDateTimeWithTimezone returns a textual representation of the unix time formatted by long date time with timezone format
func FormatUnixTimeToLongDateTimeWithTimezone(unixTime int64, timezone *time.Location) string {
t := parseFromUnixTime(unixTime)
if timezone != nil {
t = t.In(timezone)
}
return t.Format(longDateTimeWithTimezoneFormat)
}
// FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format returns a textual representation of the unix time formatted by long date time with timezone RFC 3389 format
func FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(unixTime int64, timezone *time.Location) string {
t := parseFromUnixTime(unixTime)
if timezone != nil {
t = t.In(timezone)
}
return t.Format(longDateTimeWithTimezoneRFC3389Format)
}
func FormatYearMonthDayToLongDateTime(year string, month string, day string) (string, error) {
if len(year) == 2 {
yearLast2Digits, err := StringToInt(year)
+28
View File
@@ -32,6 +32,34 @@ func TestFormatUnixTimeToLongDate(t *testing.T) {
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatUnixTimeToLongDateTimeWithTimezone(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
utc8Timezone := time.FixedZone("Test Timezone", 28800) // UTC+8
expectedValue := "2021-03-31 22:01:23Z"
actualValue := FormatUnixTimeToLongDateTimeWithTimezone(unixTime, utcTimezone)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "2021-04-01 06:01:23+08:00"
actualValue = FormatUnixTimeToLongDateTimeWithTimezone(unixTime, utc8Timezone)
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
utc8Timezone := time.FixedZone("Test Timezone", 28800) // UTC+8
expectedValue := "2021-03-31T22:01:23Z"
actualValue := FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(unixTime, utcTimezone)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "2021-04-01T06:01:23+08:00"
actualValue = FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(unixTime, utc8Timezone)
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatUnixTimeToLongDateTime(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC