akadmin revised this gist 5 months ago. Go to revision
1 file changed, 100 insertions
sample_tools.cs(file created)
| @@ -0,0 +1,100 @@ | |||
| 1 | + | using System.ComponentModel; | |
| 2 | + | using System.Data.SqlClient; | |
| 3 | + | using Microsoft.Extensions.Configuration; | |
| 4 | + | using ModelContextProtocol.Server; | |
| 5 | + | ||
| 6 | + | [McpServerToolType] | |
| 7 | + | public class DatabaseTools | |
| 8 | + | { | |
| 9 | + | private readonly IConfiguration _configuration; | |
| 10 | + | private readonly ILogger<DatabaseTools> _logger; | |
| 11 | + | ||
| 12 | + | public DatabaseTools(IConfiguration configuration, ILogger<DatabaseTools> logger) | |
| 13 | + | { | |
| 14 | + | _configuration = configuration; | |
| 15 | + | _logger = logger; | |
| 16 | + | } | |
| 17 | + | ||
| 18 | + | [McpServerTool, Description("데이터베이스에서 사용자 정보를 조회합니다.")] | |
| 19 | + | public async Task<string> GetUserInfo( | |
| 20 | + | [Description("조회할 사용자 ID")] int userId) | |
| 21 | + | { | |
| 22 | + | try | |
| 23 | + | { | |
| 24 | + | var connectionString = _configuration.GetConnectionString("DefaultConnection"); | |
| 25 | + | using var connection = new SqlConnection(connectionString); | |
| 26 | + | await connection.OpenAsync(); | |
| 27 | + | ||
| 28 | + | var query = "SELECT Id, Name, Email, CreatedDate FROM Users WHERE Id = @UserId"; | |
| 29 | + | using var command = new SqlCommand(query, connection); | |
| 30 | + | command.Parameters.AddWithValue("@UserId", userId); | |
| 31 | + | ||
| 32 | + | using var reader = await command.ExecuteReaderAsync(); | |
| 33 | + | if (await reader.ReadAsync()) | |
| 34 | + | { | |
| 35 | + | var user = new | |
| 36 | + | { | |
| 37 | + | Id = reader.GetInt32("Id"), | |
| 38 | + | Name = reader.GetString("Name"), | |
| 39 | + | Email = reader.GetString("Email"), | |
| 40 | + | CreatedDate = reader.GetDateTime("CreatedDate") | |
| 41 | + | }; | |
| 42 | + | return JsonSerializer.Serialize(user); | |
| 43 | + | } | |
| 44 | + | return "사용자를 찾을 수 없습니다."; | |
| 45 | + | } | |
| 46 | + | catch (Exception ex) | |
| 47 | + | { | |
| 48 | + | _logger.LogError(ex, "사용자 정보 조회 중 오류 발생"); | |
| 49 | + | return $"오류 발생: {ex.Message}"; | |
| 50 | + | } | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | [McpServerTool, Description("데이터베이스에서 주문 통계를 생성합니다.")] | |
| 54 | + | public async Task<string> GetOrderStatistics( | |
| 55 | + | [Description("시작 날짜 (yyyy-MM-dd)")] string startDate, | |
| 56 | + | [Description("종료 날짜 (yyyy-MM-dd)")] string endDate) | |
| 57 | + | { | |
| 58 | + | try | |
| 59 | + | { | |
| 60 | + | var connectionString = _configuration.GetConnectionString("DefaultConnection"); | |
| 61 | + | using var connection = new SqlConnection(connectionString); | |
| 62 | + | await connection.OpenAsync(); | |
| 63 | + | ||
| 64 | + | var query = @" | |
| 65 | + | SELECT | |
| 66 | + | COUNT(*) as TotalOrders, | |
| 67 | + | SUM(Amount) as TotalAmount, | |
| 68 | + | AVG(Amount) as AverageAmount, | |
| 69 | + | MAX(Amount) as MaxAmount, | |
| 70 | + | MIN(Amount) as MinAmount | |
| 71 | + | FROM Orders | |
| 72 | + | WHERE OrderDate BETWEEN @StartDate AND @EndDate"; | |
| 73 | + | ||
| 74 | + | using var command = new SqlCommand(query, connection); | |
| 75 | + | command.Parameters.AddWithValue("@StartDate", DateTime.Parse(startDate)); | |
| 76 | + | command.Parameters.AddWithValue("@EndDate", DateTime.Parse(endDate)); | |
| 77 | + | ||
| 78 | + | using var reader = await command.ExecuteReaderAsync(); | |
| 79 | + | if (await reader.ReadAsync()) | |
| 80 | + | { | |
| 81 | + | var stats = new | |
| 82 | + | { | |
| 83 | + | Period = $"{startDate} ~ {endDate}", | |
| 84 | + | TotalOrders = reader.GetInt32("TotalOrders"), | |
| 85 | + | TotalAmount = reader.GetDecimal("TotalAmount"), | |
| 86 | + | AverageAmount = reader.GetDecimal("AverageAmount"), | |
| 87 | + | MaxAmount = reader.GetDecimal("MaxAmount"), | |
| 88 | + | MinAmount = reader.GetDecimal("MinAmount") | |
| 89 | + | }; | |
| 90 | + | return JsonSerializer.Serialize(stats, new JsonSerializerOptions { WriteIndented = true }); | |
| 91 | + | } | |
| 92 | + | return "통계 데이터를 찾을 수 없습니다."; | |
| 93 | + | } | |
| 94 | + | catch (Exception ex) | |
| 95 | + | { | |
| 96 | + | _logger.LogError(ex, "주문 통계 조회 중 오류 발생"); | |
| 97 | + | return $"오류 발생: {ex.Message}"; | |
| 98 | + | } | |
| 99 | + | } | |
| 100 | + | } | |
Newer
Older