### Start MCP Server
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Starts an MCP server, automatically discovering and registering tools marked with the McpTool attribute from the base assembly.
```csharp
await MCPServer.StartAsync("CalculatorServer", "1.0.0");
```
--------------------------------
### MCPSharp Installation
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Command to add the MCPSharp NuGet package to a .NET project.
```bash
dotnet add package MCPSharp
```
--------------------------------
### MCPSharp Attributes and XML Documentation
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Illustrates how to use MCPSharp attributes like `[McpTool]`, `[McpParameter]`, and `[McpResource]` to define tools, parameters, and resources. It also shows how to leverage XML documentation comments for automatic documentation generation.
```csharp
///
/// Provides mathematical operations
///
public class Calculator
{
///
/// Adds two numbers together
///
/// The first number to add
/// The second number to add
/// The sum of the two numbers
[McpTool]
public static int Add(
[McpParameter(true)] int a,
[McpParameter(true)] int b)
{
return a + b;
}
}
```
--------------------------------
### Enabling XML Documentation Generation
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Shows the necessary project file configuration to enable the generation of XML documentation files, which MCPSharp uses to extract tool and parameter information.
```xml
true
$(NoWarn);1591
```
--------------------------------
### Dynamic Tool Registration
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Shows how to dynamically register a tool with a custom handler. This allows for on-the-fly registration of tools with specific input schemas and execution logic.
```csharp
MCPServer.AddToolHandler(new Tool()
{
Name = "dynamicTool",
Description = "A dynamic tool",
InputSchema = new InputSchema {
Type = "object",
Required = ["input"],
Properties = new Dictionary{
{"input", new ParameterSchema{Type="string", Description="Input value"}}
}
}
}, (string input) => { return $"You provided: {input}"; });
```
--------------------------------
### MCPSharp API Reference
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Provides a detailed reference for MCPSharp attributes, server methods, and client methods, outlining their purpose, parameters, and usage.
```APIDOC
Attributes:
- [McpTool]: Marks a class or method as an MCP tool. Optional parameters: Name (tool name, default: class/method name), Description (tool description).
- [McpParameter]: Provides metadata for function parameters. Optional parameters: Description (parameter description), Required (whether parameter is required, default: false).
- [McpResource]: Marks a property or method as an MCP resource. Parameters: Name (resource name), Uri (resource URI, can include templates), MimeType (resource MIME type), Description (resource description).
Server Methods:
- MCPServer.StartAsync(string serverName, string version): Starts the MCP server.
- MCPServer.Register(): Registers a class containing tools or resources.
- MCPServer.AddToolHandler(Tool tool, Delegate func): Registers a dynamic tool.
Client Methods:
- new MCPClient(string name, string version, string server, string args = null, IDictionary env = null): Create a client instance.
- client.GetToolsAsync(): Get available tools.
- client.CallToolAsync(string name, Dictionary parameters): Call a tool.
- client.GetResourcesAsync(): Get available resources.
- client.GetFunctionsAsync(): Get tools as AIFunctions.
```
--------------------------------
### MCP Client Integration with Microsoft.Extensions.AI
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Demonstrates client-side integration with Microsoft.Extensions.AI, retrieving available functions from an MCP server to be used with an IChatClient.
```csharp
// Client-side integration
MCPClient client = new("AIClient", "1.0", "path/to/mcp/server");
IList functions = await client.GetFunctionsAsync();
// This list can be plugged into the ChatOptions.Tools property for an IChatClient,
// Allowing MCP servers to be used seamlessly with Any IChatClient Implementation.
```
--------------------------------
### Semantic Kernel Integration with MCP Server
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Demonstrates how to create a Semantic Kernel skill class and register it with the MCP server. This is the primary method for making Semantic Kernel functions available to the MCP server.
```csharp
using Microsoft.SemanticKernel;
public class MySkillClass
{
[KernelFunction("MyFunction")]
[Description("Description of my function")]
public string MyFunction(string input) => $"Processed: {input}";
}
// Register with MCPServer
MCPServer.Register();
```
--------------------------------
### Define an MCP Tool
Source: https://github.com/afrise/mcpsharp/blob/master/README.md
Demonstrates how to define a tool using the McpTool attribute for AI model interaction. The McpParameter attribute specifies required parameters.
```csharp
using MCPSharp;
public class Calculator
{
[McpTool("add", "Adds two numbers")] // Note: [McpFunction] is deprecated, use [McpTool] instead
public static int Add([McpParameter(true)] int a, [McpParameter(true)] int b)
{
return a + b;
}
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.