Skip to content

Commit 189605b

Browse files
Cleanup
1 parent 47b4dff commit 189605b

5 files changed

Lines changed: 42 additions & 46 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<ItemGroup>
2020
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
2121
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.9" />
22-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
22+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
2323
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
2424
<PackageVersion Include="ModelContextProtocol" Version="0.4.0-preview.1" />
2525
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.4.0-preview.1" />

McpDotNetSample.Asp/Services/InMemoryGithubUserRepository.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@ namespace McpDotNetSample.Asp.Services;
66
/// </summary>
77
public class InMemoryGithubUserRepository : IGithubUserRepository
88
{
9-
// Custom comparer for case-insensitive tuple comparison
10-
private class NameComparer : IEqualityComparer<(string FirstName, string LastName)>
11-
{
12-
public bool Equals((string FirstName, string LastName) x, (string FirstName, string LastName) y)
13-
{
14-
return string.Equals(x.FirstName, y.FirstName, StringComparison.OrdinalIgnoreCase) &&
15-
string.Equals(x.LastName, y.LastName, StringComparison.OrdinalIgnoreCase);
16-
}
17-
18-
public int GetHashCode((string FirstName, string LastName) obj)
19-
{
20-
return HashCode.Combine(
21-
obj.FirstName?.ToLowerInvariant(),
22-
obj.LastName?.ToLowerInvariant());
23-
}
24-
}
25-
269
// Dictionary mapping: (FirstName, LastName) -> GitHubUsername
2710
private readonly Dictionary<(string FirstName, string LastName), string> _users = new(new NameComparer())
2811
{
@@ -40,7 +23,7 @@ public int GetHashCode((string FirstName, string LastName) obj)
4023

4124
public IEnumerable<string> FindByFirstName(string firstName)
4225
{
43-
ArgumentNullException.ThrowIfNullOrWhiteSpace(firstName);
26+
ArgumentException.ThrowIfNullOrWhiteSpace(firstName);
4427

4528
return _users
4629
.Where(kvp => kvp.Key.FirstName.Equals(firstName, StringComparison.OrdinalIgnoreCase))
@@ -50,7 +33,7 @@ public IEnumerable<string> FindByFirstName(string firstName)
5033

5134
public IEnumerable<string> FindByLastName(string lastName)
5235
{
53-
ArgumentNullException.ThrowIfNullOrWhiteSpace(lastName);
36+
ArgumentException.ThrowIfNullOrWhiteSpace(lastName);
5437

5538
return _users
5639
.Where(kvp => kvp.Key.LastName.Equals(lastName, StringComparison.OrdinalIgnoreCase))
@@ -60,10 +43,27 @@ public IEnumerable<string> FindByLastName(string lastName)
6043

6144
public string? FindByFullName(string firstName, string lastName)
6245
{
63-
ArgumentNullException.ThrowIfNullOrWhiteSpace(firstName);
64-
ArgumentNullException.ThrowIfNullOrWhiteSpace(lastName);
46+
ArgumentException.ThrowIfNullOrWhiteSpace(firstName);
47+
ArgumentException.ThrowIfNullOrWhiteSpace(lastName);
6548

6649
var key = (firstName, lastName);
6750
return _users.TryGetValue(key, out var username) ? username : null;
6851
}
52+
53+
// Custom comparer for case-insensitive tuple comparison
54+
private class NameComparer : IEqualityComparer<(string FirstName, string LastName)>
55+
{
56+
public bool Equals((string FirstName, string LastName) x, (string FirstName, string LastName) y)
57+
{
58+
return string.Equals(x.FirstName, y.FirstName, StringComparison.OrdinalIgnoreCase) &&
59+
string.Equals(x.LastName, y.LastName, StringComparison.OrdinalIgnoreCase);
60+
}
61+
62+
public int GetHashCode((string FirstName, string LastName) obj)
63+
{
64+
return HashCode.Combine(
65+
obj.FirstName?.ToLowerInvariant(),
66+
obj.LastName?.ToLowerInvariant());
67+
}
68+
}
6969
}

McpDotNetSample.Asp/Tools/GithubUsersIKnowTool.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class GithubUsersIKnowTool
1212
[McpServerTool, Description("Look up the Github username of someone in your organization based on their name.")]
1313
public static string LookupGithubUsername(IGithubUserRepository githubUserRepository, string name)
1414
{
15-
ArgumentNullException.ThrowIfNullOrWhiteSpace(name);
15+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
1616

1717
if (githubUserRepository is null)
1818
{
@@ -61,12 +61,9 @@ public static string LookupGithubUsername(IGithubUserRepository githubUserReposi
6161
// Try searching by last name as a fallback
6262
var lastNameMatches = githubUserRepository.FindByLastName(lastName).ToList();
6363

64-
if (lastNameMatches.Count > 0)
65-
{
66-
return $"No match for first name '{firstName}'. Users with last name '{lastName}': {string.Join(", ", lastNameMatches)}";
67-
}
68-
69-
return $"No GitHub user found matching '{name}'.";
64+
return lastNameMatches.Count > 0
65+
? $"No match for first name '{firstName}'. Users with last name '{lastName}': {string.Join(", ", lastNameMatches)}"
66+
: $"No GitHub user found matching '{name}'.";
7067
}
7168
}
7269
}

McpDotNetSample/Program.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66

77
using ModelContextProtocol.Server;
88

9-
public class Program
9+
namespace McpDotNetSample;
10+
11+
public static class Program
1012
{
1113
public static async Task Main(string[] args)
1214
{
1315
var builder = Host.CreateApplicationBuilder(args);
1416
builder.Logging.AddConsole(consoleLogOptions =>
15-
{
16-
// Configure all logs to go to stderr
17-
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
18-
});
17+
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace);
1918
builder.Services
2019
.AddMcpServer()
2120
.WithStdioServerTransport()
@@ -25,11 +24,11 @@ public static async Task Main(string[] args)
2524
}
2625

2726
[McpServerToolType]
28-
public class AddTool
27+
public static class AddTool
2928
{
3029
[McpServerTool(Name = "add"), Description("Adds two numbers.")]
31-
public static string Add(int a, int b)
30+
public static string Add(int left, int right)
3231
{
33-
return $"The sum of {a} and {b} is {a + b}";
32+
return $"The sum of {left} and {right} is {left + right}";
3433
}
3534
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
namespace McpDotNetSample.Tools;
77

88
[McpServerToolType]
9-
public static class SnippetTool
9+
public static class CodeSnippetTool
1010
{
1111
// Root directory for snippet storage (temp folder to keep things ephemeral per-machine)
1212
private static readonly string SnippetRoot = Path.Combine(Path.GetTempPath(), "mcp_snippets");
1313

1414
[McpServerTool, Description("Retrieves a code snippet by name. Returns the snippet content.")]
15-
public static string GetSnippet(string name)
15+
public static string GetCodeSnippet(string name)
1616
{
17-
ArgumentNullException.ThrowIfNullOrWhiteSpace(name);
17+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
1818

1919
EnsureRoot();
2020
string path = GetFilePath(name);
@@ -27,10 +27,10 @@ public static string GetSnippet(string name)
2727
}
2828

2929
[McpServerTool, Description("Saves (creates or overwrites) a code snippet by name and returns metadata.")]
30-
public static Snippet SaveSnippet(string name, string content)
30+
public static Snippet SaveCodeSnippet(string name, string content)
3131
{
32-
ArgumentNullException.ThrowIfNullOrWhiteSpace(name);
33-
ArgumentNullException.ThrowIfNullOrWhiteSpace(content);
32+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
33+
ArgumentException.ThrowIfNullOrWhiteSpace(content);
3434

3535
EnsureRoot();
3636
var path = GetFilePath(name);
@@ -43,17 +43,17 @@ public static Snippet SaveSnippet(string name, string content)
4343
}
4444

4545
[McpServerTool, Description("Lists available snippet names in the collection.")]
46-
public static string[] ListSnippets()
46+
public static string[] ListCodeSnippets()
4747
{
4848
EnsureRoot();
4949
var files = Directory.EnumerateFiles(SnippetRoot, "*.json", SearchOption.TopDirectoryOnly);
5050
return [.. files.Select(f => Path.GetFileNameWithoutExtension(f))];
5151
}
5252

5353
[McpServerTool, Description("Deletes a code snippet by name.")]
54-
public static void DeleteSnippet(string name)
54+
public static void DeleteCodeSnippet(string name)
5555
{
56-
ArgumentNullException.ThrowIfNullOrWhiteSpace(name);
56+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
5757

5858
EnsureRoot();
5959
var path = GetFilePath(name);

0 commit comments

Comments
 (0)