bluetsys/csharp-mcp-server-sample
If you are the rightful owner of csharp-mcp-server-sample and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
Csharp MCP Server Sample is a utility for fetching data from the JsonPlaceholder API, designed to work with .NET 6 and above.
Csharp MCP Server Sample
개요
Csharp MCP Server Sample는 JsonPlaceholder(https://jsonplaceholder.typicode.com/) API를 호출하여 데이터를 가져오는 유틸리티를 제공합니다. 이 프로젝트는 .NET 6 이상에서 동작하며, 콘솔 애플리케이션 또는 ASP.NET Core 프로젝트에서 쉽게 통합하여 사용할 수 있습니다.
권장 사항
- .NET 10.0 SDK
- Visual Studio Code
- Visual Studio Code용 GitHub Copilot 확장
.NET 10.0 SDK 설치는 여기를 참고하세요.
주요 기능
- JsonPlaceholder API 호출을 위한 HttpClient 생성 및 관리
- 다양한 엔드포인트(
/posts,/comments,/albums,/photos,/todos,/users)에 대한 데이터 조회 기능 제공 - 단건 조회 및 목록 조회 지원
지원 엔드포인트
| 엔드포인트 | 설명 | 단건 조회 메서드 | 목록 조회 메서드 |
|---|---|---|---|
/posts | 게시글 데이터 | GetPostAsync | GetPostListAsync |
/comments | 댓글 데이터 | GetCommentAsync | GetCommentListAsync |
/albums | 앨범 데이터 | GetAlbumAsync | GetAlbumListAsync |
/photos | 사진 데이터 | GetPhotoAsync | GetPhotoListAsync |
/todos | 할 일(Todo) 데이터 | GetTodoAsync | GetTodoListAsync |
/users | 사용자 데이터 | GetUserAsync | GetUserListAsync |
사용 방법
1. HttpClient 생성
CreateJsonPlaceholderClient 메서드를 사용하여 JsonPlaceholder 전용 HttpClient를 생성합니다. 기본적으로 BaseAddress는 JsonPlaceholder API로 설정되며, 공통 헤더가 적용됩니다.
private HttpClient CreateJsonPlaceholderClient(
TimeSpan? timeout = null,
string? userAgent = "JsonPlaceholderSample/1.0",
string? bearerToken = null,
SocketsHttpHandler? handler = null)
2. 단건 조회
특정 ID에 해당하는 데이터를 조회하려면 아래 메서드를 사용합니다.
예: GetPostAsync
[McpServerTool]
[Description("Post 단건 조회 (JSON 반환)")]
public async Task<string> GetPostAsync(int id)
3. 목록 조회
전체 데이터를 조회하려면 아래 메서드를 사용합니다.
예: GetPostListAsync
[McpServerTool]
[Description("Post 목록 조회 (JSON 반환)")]
public async Task<string> GetPostListAsync()
설치 및 실행
1. 프로젝트 빌드
pwsh
> dotnet build
2. 실행
Program.cs에서 MCP 서버를 실행합니다.
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithTools<SampleTools>();
await builder.Build().RunAsync();
3. MCP 도구 사용
SampleTools 클래스에 정의된 MCP 도구를 통해 JsonPlaceholder API를 호출할 수 있습니다.
참고 자료 및 권장 사항
참고 자료
- 자세한 사항은 Model Context Protocol 공식 사이트를 참고하세요.
- 본 소스코드는 Microsoft 공식 문서를 참고하여 작성되었습니다.
- 위 문서의 내용은 GitHub 저장소에 해당하는 MS 공식 문서입니다.
다른 언어로 작성하기
다른 언어로 MCP 서버를 작성하려면 아래 문서를 참고하세요:
.vscode/mcp.json 파일 설명
전체
- Public-Data-Search: PHP HTTP로 작성된 공공데이터 검색 예제 (소스 코드는 미공개)
- Sample-McpServer: 본 샘플 파일의 MCP 설정 (소스 코드 참고)
기능
- 시작: 해당 MCP를 실행 한다.
- 실행중: 해당 MCP의 로그를 본다.( 오류 로그등을 표시 할 수 있다)
- 중지: 해당 MCP를 중지 한다.
- 다시시작: 해당 MCP를 다시 시작한다. ( 코드 수정 후 새로 적용 하기 위해 사용 )
자세한 사항은 Visual Studio Code MCP 서버 문서를 참고하세요.
결론
- MCP Server를 등록 할 수 있는 agent나 LLM 서비스등에서 다양하게 MCP 를 작성 해서 활요 하세여
라이선스
이 프로젝트는 오픈소스 라이선스를 따릅니다. 자세한 내용은 LICENSE 파일을 참조하세요.