Fix tools visibility - revert to v0.1.6 authentication approach
- Disable issuer validation in BearerAuthProvider (issuer=None) - Simplify authentication condition (remove auth_enabled check) - Revert CORS middleware to simple configuration - Fix OAuth metadata endpoint to match v0.1.6 - Apply conditional auth only to MCP server creation Critical fixes for Claude AI tools discovery
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
# sayistay_mcp_module/__init__.py
|
||||
|
||||
"""
|
||||
Sayıştay (Turkish Court of Accounts) MCP Module
|
||||
|
||||
This module provides access to three types of Sayıştay decisions:
|
||||
- Genel Kurul (General Assembly) decisions
|
||||
- Temyiz Kurulu (Appeals Board) decisions
|
||||
- Daire (Chamber) decisions
|
||||
|
||||
The module handles ASP.NET WebForms authentication with CSRF tokens
|
||||
and DataTables-based pagination for comprehensive decision search.
|
||||
"""
|
||||
|
||||
from .client import SayistayApiClient
|
||||
from .models import (
|
||||
# Genel Kurul models
|
||||
GenelKurulSearchRequest,
|
||||
GenelKurulSearchResponse,
|
||||
GenelKurulDecision,
|
||||
|
||||
# Temyiz Kurulu models
|
||||
TemyizKuruluSearchRequest,
|
||||
TemyizKuruluSearchResponse,
|
||||
TemyizKuruluDecision,
|
||||
|
||||
# Daire models
|
||||
DaireSearchRequest,
|
||||
DaireSearchResponse,
|
||||
DaireDecision,
|
||||
|
||||
# Document models
|
||||
SayistayDocumentMarkdown
|
||||
)
|
||||
from .enums import (
|
||||
DaireEnum,
|
||||
KamuIdaresiTuruEnum,
|
||||
WebKararKonusuEnum
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"SayistayApiClient",
|
||||
"GenelKurulSearchRequest",
|
||||
"GenelKurulSearchResponse",
|
||||
"GenelKurulDecision",
|
||||
"TemyizKuruluSearchRequest",
|
||||
"TemyizKuruluSearchResponse",
|
||||
"TemyizKuruluDecision",
|
||||
"DaireSearchRequest",
|
||||
"DaireSearchResponse",
|
||||
"DaireDecision",
|
||||
"SayistayDocumentMarkdown",
|
||||
"DaireEnum",
|
||||
"KamuIdaresiTuruEnum",
|
||||
"WebKararKonusuEnum"
|
||||
]
|
||||
@@ -0,0 +1,689 @@
|
||||
# sayistay_mcp_module/client.py
|
||||
|
||||
import httpx
|
||||
import re
|
||||
from bs4 import BeautifulSoup
|
||||
from typing import Dict, Any, List, Optional, Tuple
|
||||
import logging
|
||||
import html
|
||||
import io
|
||||
from urllib.parse import urlencode, urljoin
|
||||
from markitdown import MarkItDown
|
||||
|
||||
from .models import (
|
||||
GenelKurulSearchRequest, GenelKurulSearchResponse, GenelKurulDecision,
|
||||
TemyizKuruluSearchRequest, TemyizKuruluSearchResponse, TemyizKuruluDecision,
|
||||
DaireSearchRequest, DaireSearchResponse, DaireDecision,
|
||||
SayistayDocumentMarkdown
|
||||
)
|
||||
from .enums import DaireEnum, KamuIdaresiTuruEnum, WebKararKonusuEnum, WEB_KARAR_KONUSU_MAPPING
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
if not logger.hasHandlers():
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
class SayistayApiClient:
|
||||
"""
|
||||
API Client for Sayıştay (Turkish Court of Accounts) decision search system.
|
||||
|
||||
Handles three types of decisions:
|
||||
- Genel Kurul (General Assembly): Precedent-setting interpretive decisions
|
||||
- Temyiz Kurulu (Appeals Board): Appeals against chamber decisions
|
||||
- Daire (Chamber): First-instance audit findings and sanctions
|
||||
|
||||
Features:
|
||||
- ASP.NET WebForms session management with CSRF tokens
|
||||
- DataTables-based pagination and filtering
|
||||
- Automatic session refresh on expiration
|
||||
- Document retrieval with Markdown conversion
|
||||
"""
|
||||
|
||||
BASE_URL = "https://www.sayistay.gov.tr"
|
||||
|
||||
# Search endpoints for each decision type
|
||||
GENEL_KURUL_ENDPOINT = "/KararlarGenelKurul/DataTablesList"
|
||||
TEMYIZ_KURULU_ENDPOINT = "/KararlarTemyiz/DataTablesList"
|
||||
DAIRE_ENDPOINT = "/KararlarDaire/DataTablesList"
|
||||
|
||||
# Page endpoints for session initialization and document access
|
||||
GENEL_KURUL_PAGE = "/KararlarGenelKurul"
|
||||
TEMYIZ_KURULU_PAGE = "/KararlarTemyiz"
|
||||
DAIRE_PAGE = "/KararlarDaire"
|
||||
|
||||
def __init__(self, request_timeout: float = 60.0):
|
||||
self.request_timeout = request_timeout
|
||||
self.session_cookies: Dict[str, str] = {}
|
||||
self.csrf_tokens: Dict[str, str] = {} # Store tokens for each endpoint
|
||||
|
||||
self.http_client = httpx.AsyncClient(
|
||||
base_url=self.BASE_URL,
|
||||
headers={
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"Accept-Language": "tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin"
|
||||
},
|
||||
timeout=request_timeout,
|
||||
follow_redirects=True
|
||||
)
|
||||
|
||||
async def _initialize_session_for_endpoint(self, endpoint_type: str) -> bool:
|
||||
"""
|
||||
Initialize session and obtain CSRF token for specific endpoint.
|
||||
|
||||
Args:
|
||||
endpoint_type: One of 'genel_kurul', 'temyiz_kurulu', 'daire'
|
||||
|
||||
Returns:
|
||||
True if session initialized successfully, False otherwise
|
||||
"""
|
||||
page_mapping = {
|
||||
'genel_kurul': self.GENEL_KURUL_PAGE,
|
||||
'temyiz_kurulu': self.TEMYIZ_KURULU_PAGE,
|
||||
'daire': self.DAIRE_PAGE
|
||||
}
|
||||
|
||||
if endpoint_type not in page_mapping:
|
||||
logger.error(f"Invalid endpoint type: {endpoint_type}")
|
||||
return False
|
||||
|
||||
page_url = page_mapping[endpoint_type]
|
||||
logger.info(f"Initializing session for {endpoint_type} endpoint: {page_url}")
|
||||
|
||||
try:
|
||||
response = await self.http_client.get(page_url)
|
||||
response.raise_for_status()
|
||||
|
||||
# Extract session cookies
|
||||
for cookie_name, cookie_value in response.cookies.items():
|
||||
self.session_cookies[cookie_name] = cookie_value
|
||||
logger.debug(f"Stored session cookie: {cookie_name}")
|
||||
|
||||
# Extract CSRF token from form
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
csrf_input = soup.find('input', {'name': '__RequestVerificationToken'})
|
||||
|
||||
if csrf_input and csrf_input.get('value'):
|
||||
self.csrf_tokens[endpoint_type] = csrf_input['value']
|
||||
logger.info(f"Extracted CSRF token for {endpoint_type}")
|
||||
return True
|
||||
else:
|
||||
logger.warning(f"CSRF token not found in {endpoint_type} page")
|
||||
return False
|
||||
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"HTTP error during session initialization for {endpoint_type}: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Error initializing session for {endpoint_type}: {e}")
|
||||
return False
|
||||
|
||||
def _enum_to_form_value(self, enum_value: str, enum_type: str) -> str:
|
||||
"""Convert enum values to form values expected by the API."""
|
||||
if enum_value == "ALL":
|
||||
if enum_type == "daire":
|
||||
return "Tüm Daireler"
|
||||
elif enum_type == "kamu_idaresi":
|
||||
return "Tüm Kurumlar"
|
||||
elif enum_type == "web_karar_konusu":
|
||||
return "Tüm Konular"
|
||||
|
||||
# Apply web_karar_konusu mapping
|
||||
if enum_type == "web_karar_konusu":
|
||||
return WEB_KARAR_KONUSU_MAPPING.get(enum_value, enum_value)
|
||||
|
||||
return enum_value
|
||||
|
||||
def _build_datatables_params(self, start: int, length: int, draw: int = 1) -> List[Tuple[str, str]]:
|
||||
"""Build standard DataTables parameters for all endpoints."""
|
||||
params = [
|
||||
("draw", str(draw)),
|
||||
("start", str(start)),
|
||||
("length", str(length)),
|
||||
("search[value]", ""),
|
||||
("search[regex]", "false")
|
||||
]
|
||||
return params
|
||||
|
||||
def _build_genel_kurul_form_data(self, params: GenelKurulSearchRequest, draw: int = 1) -> List[Tuple[str, str]]:
|
||||
"""Build form data for Genel Kurul search request."""
|
||||
form_data = self._build_datatables_params(params.start, params.length, draw)
|
||||
|
||||
# Add DataTables column definitions (from actual request)
|
||||
column_defs = [
|
||||
("columns[0][data]", "KARARNO"),
|
||||
("columns[0][name]", ""),
|
||||
("columns[0][searchable]", "true"),
|
||||
("columns[0][orderable]", "false"),
|
||||
("columns[0][search][value]", ""),
|
||||
("columns[0][search][regex]", "false"),
|
||||
|
||||
("columns[1][data]", "KARARNO"),
|
||||
("columns[1][name]", ""),
|
||||
("columns[1][searchable]", "true"),
|
||||
("columns[1][orderable]", "true"),
|
||||
("columns[1][search][value]", ""),
|
||||
("columns[1][search][regex]", "false"),
|
||||
|
||||
("columns[2][data]", "KARARTARIH"),
|
||||
("columns[2][name]", ""),
|
||||
("columns[2][searchable]", "true"),
|
||||
("columns[2][orderable]", "true"),
|
||||
("columns[2][search][value]", ""),
|
||||
("columns[2][search][regex]", "false"),
|
||||
|
||||
("columns[3][data]", "KARAROZETI"),
|
||||
("columns[3][name]", ""),
|
||||
("columns[3][searchable]", "true"),
|
||||
("columns[3][orderable]", "false"),
|
||||
("columns[3][search][value]", ""),
|
||||
("columns[3][search][regex]", "false"),
|
||||
|
||||
("columns[4][data]", ""),
|
||||
("columns[4][name]", ""),
|
||||
("columns[4][searchable]", "true"),
|
||||
("columns[4][orderable]", "false"),
|
||||
("columns[4][search][value]", ""),
|
||||
("columns[4][search][regex]", "false"),
|
||||
|
||||
("order[0][column]", "2"),
|
||||
("order[0][dir]", "desc")
|
||||
]
|
||||
form_data.extend(column_defs)
|
||||
|
||||
# Add search parameters
|
||||
form_data.extend([
|
||||
("KararlarGenelKurulAra.KARARNO", params.karar_no or ""),
|
||||
("__Invariant[]", "KararlarGenelKurulAra.KARARNO"),
|
||||
("__Invariant[]", "KararlarGenelKurulAra.KARAREK"),
|
||||
("KararlarGenelKurulAra.KARAREK", params.karar_ek or ""),
|
||||
("KararlarGenelKurulAra.KARARTARIHBaslangic", params.karar_tarih_baslangic or "Başlangıç Tarihi"),
|
||||
("KararlarGenelKurulAra.KARARTARIHBitis", params.karar_tarih_bitis or "Bitiş Tarihi"),
|
||||
("KararlarGenelKurulAra.KARARTAMAMI", params.karar_tamami or ""),
|
||||
("__RequestVerificationToken", self.csrf_tokens.get('genel_kurul', ''))
|
||||
])
|
||||
|
||||
return form_data
|
||||
|
||||
def _build_temyiz_kurulu_form_data(self, params: TemyizKuruluSearchRequest, draw: int = 1) -> List[Tuple[str, str]]:
|
||||
"""Build form data for Temyiz Kurulu search request."""
|
||||
form_data = self._build_datatables_params(params.start, params.length, draw)
|
||||
|
||||
# Add DataTables column definitions (from actual request)
|
||||
column_defs = [
|
||||
("columns[0][data]", "TEMYIZTUTANAKTARIHI"),
|
||||
("columns[0][name]", ""),
|
||||
("columns[0][searchable]", "true"),
|
||||
("columns[0][orderable]", "false"),
|
||||
("columns[0][search][value]", ""),
|
||||
("columns[0][search][regex]", "false"),
|
||||
|
||||
("columns[1][data]", "TEMYIZTUTANAKTARIHI"),
|
||||
("columns[1][name]", ""),
|
||||
("columns[1][searchable]", "true"),
|
||||
("columns[1][orderable]", "true"),
|
||||
("columns[1][search][value]", ""),
|
||||
("columns[1][search][regex]", "false"),
|
||||
|
||||
("columns[2][data]", "ILAMDAIRESI"),
|
||||
("columns[2][name]", ""),
|
||||
("columns[2][searchable]", "true"),
|
||||
("columns[2][orderable]", "true"),
|
||||
("columns[2][search][value]", ""),
|
||||
("columns[2][search][regex]", "false"),
|
||||
|
||||
("columns[3][data]", "TEMYIZKARAR"),
|
||||
("columns[3][name]", ""),
|
||||
("columns[3][searchable]", "true"),
|
||||
("columns[3][orderable]", "false"),
|
||||
("columns[3][search][value]", ""),
|
||||
("columns[3][search][regex]", "false"),
|
||||
|
||||
("columns[4][data]", ""),
|
||||
("columns[4][name]", ""),
|
||||
("columns[4][searchable]", "true"),
|
||||
("columns[4][orderable]", "false"),
|
||||
("columns[4][search][value]", ""),
|
||||
("columns[4][search][regex]", "false"),
|
||||
|
||||
("order[0][column]", "1"),
|
||||
("order[0][dir]", "desc")
|
||||
]
|
||||
form_data.extend(column_defs)
|
||||
|
||||
# Add search parameters
|
||||
daire_value = self._enum_to_form_value(params.ilam_dairesi, "daire")
|
||||
kamu_idaresi_value = self._enum_to_form_value(params.kamu_idaresi_turu, "kamu_idaresi")
|
||||
web_karar_konusu_value = self._enum_to_form_value(params.web_karar_konusu, "web_karar_konusu")
|
||||
|
||||
form_data.extend([
|
||||
("KararlarTemyizAra.ILAMDAIRESI", daire_value),
|
||||
("KararlarTemyizAra.YILI", params.yili or ""),
|
||||
("KararlarTemyizAra.KARARTRHBaslangic", params.karar_tarih_baslangic or ""),
|
||||
("KararlarTemyizAra.KARARTRHBitis", params.karar_tarih_bitis or ""),
|
||||
("KararlarTemyizAra.KAMUIDARESITURU", kamu_idaresi_value if kamu_idaresi_value != "Tüm Kurumlar" else ""),
|
||||
("KararlarTemyizAra.ILAMNO", params.ilam_no or ""),
|
||||
("KararlarTemyizAra.DOSYANO", params.dosya_no or ""),
|
||||
("KararlarTemyizAra.TEMYIZTUTANAKNO", params.temyiz_tutanak_no or ""),
|
||||
("__Invariant", "KararlarTemyizAra.TEMYIZTUTANAKNO"),
|
||||
("KararlarTemyizAra.TEMYIZKARAR", params.temyiz_karar or ""),
|
||||
("KararlarTemyizAra.WEBKARARKONUSU", web_karar_konusu_value if web_karar_konusu_value != "Tüm Konular" else ""),
|
||||
("__RequestVerificationToken", self.csrf_tokens.get('temyiz_kurulu', ''))
|
||||
])
|
||||
|
||||
return form_data
|
||||
|
||||
def _build_daire_form_data(self, params: DaireSearchRequest, draw: int = 1) -> List[Tuple[str, str]]:
|
||||
"""Build form data for Daire search request."""
|
||||
form_data = self._build_datatables_params(params.start, params.length, draw)
|
||||
|
||||
# Add DataTables column definitions (from actual request)
|
||||
column_defs = [
|
||||
("columns[0][data]", "YARGILAMADAIRESI"),
|
||||
("columns[0][name]", ""),
|
||||
("columns[0][searchable]", "true"),
|
||||
("columns[0][orderable]", "false"),
|
||||
("columns[0][search][value]", ""),
|
||||
("columns[0][search][regex]", "false"),
|
||||
|
||||
("columns[1][data]", "KARARTRH"),
|
||||
("columns[1][name]", ""),
|
||||
("columns[1][searchable]", "true"),
|
||||
("columns[1][orderable]", "true"),
|
||||
("columns[1][search][value]", ""),
|
||||
("columns[1][search][regex]", "false"),
|
||||
|
||||
("columns[2][data]", "KARARNO"),
|
||||
("columns[2][name]", ""),
|
||||
("columns[2][searchable]", "true"),
|
||||
("columns[2][orderable]", "true"),
|
||||
("columns[2][search][value]", ""),
|
||||
("columns[2][search][regex]", "false"),
|
||||
|
||||
("columns[3][data]", "YARGILAMADAIRESI"),
|
||||
("columns[3][name]", ""),
|
||||
("columns[3][searchable]", "true"),
|
||||
("columns[3][orderable]", "true"),
|
||||
("columns[3][search][value]", ""),
|
||||
("columns[3][search][regex]", "false"),
|
||||
|
||||
("columns[4][data]", "WEBKARARMETNI"),
|
||||
("columns[4][name]", ""),
|
||||
("columns[4][searchable]", "true"),
|
||||
("columns[4][orderable]", "false"),
|
||||
("columns[4][search][value]", ""),
|
||||
("columns[4][search][regex]", "false"),
|
||||
|
||||
("columns[5][data]", ""),
|
||||
("columns[5][name]", ""),
|
||||
("columns[5][searchable]", "true"),
|
||||
("columns[5][orderable]", "false"),
|
||||
("columns[5][search][value]", ""),
|
||||
("columns[5][search][regex]", "false"),
|
||||
|
||||
("order[0][column]", "2"),
|
||||
("order[0][dir]", "desc")
|
||||
]
|
||||
form_data.extend(column_defs)
|
||||
|
||||
# Add search parameters
|
||||
daire_value = self._enum_to_form_value(params.yargilama_dairesi, "daire")
|
||||
kamu_idaresi_value = self._enum_to_form_value(params.kamu_idaresi_turu, "kamu_idaresi")
|
||||
web_karar_konusu_value = self._enum_to_form_value(params.web_karar_konusu, "web_karar_konusu")
|
||||
|
||||
form_data.extend([
|
||||
("KararlarDaireAra.YARGILAMADAIRESI", daire_value),
|
||||
("KararlarDaireAra.KARARTRHBaslangic", params.karar_tarih_baslangic or ""),
|
||||
("KararlarDaireAra.KARARTRHBitis", params.karar_tarih_bitis or ""),
|
||||
("KararlarDaireAra.ILAMNO", params.ilam_no or ""),
|
||||
("KararlarDaireAra.KAMUIDARESITURU", kamu_idaresi_value if kamu_idaresi_value != "Tüm Kurumlar" else ""),
|
||||
("KararlarDaireAra.HESAPYILI", params.hesap_yili or ""),
|
||||
("KararlarDaireAra.WEBKARARKONUSU", web_karar_konusu_value if web_karar_konusu_value != "Tüm Konular" else ""),
|
||||
("KararlarDaireAra.WEBKARARMETNI", params.web_karar_metni or ""),
|
||||
("__RequestVerificationToken", self.csrf_tokens.get('daire', ''))
|
||||
])
|
||||
|
||||
return form_data
|
||||
|
||||
async def search_genel_kurul_decisions(self, params: GenelKurulSearchRequest) -> GenelKurulSearchResponse:
|
||||
"""
|
||||
Search Sayıştay Genel Kurul (General Assembly) decisions.
|
||||
|
||||
Args:
|
||||
params: Search parameters for Genel Kurul decisions
|
||||
|
||||
Returns:
|
||||
GenelKurulSearchResponse with matching decisions
|
||||
"""
|
||||
# Initialize session if needed
|
||||
if 'genel_kurul' not in self.csrf_tokens:
|
||||
if not await self._initialize_session_for_endpoint('genel_kurul'):
|
||||
raise Exception("Failed to initialize session for Genel Kurul endpoint")
|
||||
|
||||
form_data = self._build_genel_kurul_form_data(params)
|
||||
encoded_data = urlencode(form_data, encoding='utf-8')
|
||||
|
||||
logger.info(f"Searching Genel Kurul decisions with parameters: {params.model_dump(exclude_none=True)}")
|
||||
|
||||
try:
|
||||
# Update headers with cookies
|
||||
headers = self.http_client.headers.copy()
|
||||
if self.session_cookies:
|
||||
cookie_header = "; ".join([f"{k}={v}" for k, v in self.session_cookies.items()])
|
||||
headers["Cookie"] = cookie_header
|
||||
|
||||
response = await self.http_client.post(
|
||||
self.GENEL_KURUL_ENDPOINT,
|
||||
data=encoded_data,
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
response_json = response.json()
|
||||
|
||||
# Parse response
|
||||
decisions = []
|
||||
for item in response_json.get('data', []):
|
||||
decisions.append(GenelKurulDecision(
|
||||
id=item['Id'],
|
||||
karar_no=item['KARARNO'],
|
||||
karar_tarih=item['KARARTARIH'],
|
||||
karar_ozeti=item['KARAROZETI']
|
||||
))
|
||||
|
||||
return GenelKurulSearchResponse(
|
||||
decisions=decisions,
|
||||
total_records=response_json.get('recordsTotal', 0),
|
||||
total_filtered=response_json.get('recordsFiltered', 0),
|
||||
draw=response_json.get('draw', 1)
|
||||
)
|
||||
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"HTTP error during Genel Kurul search: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing Genel Kurul search: {e}")
|
||||
raise
|
||||
|
||||
async def search_temyiz_kurulu_decisions(self, params: TemyizKuruluSearchRequest) -> TemyizKuruluSearchResponse:
|
||||
"""
|
||||
Search Sayıştay Temyiz Kurulu (Appeals Board) decisions.
|
||||
|
||||
Args:
|
||||
params: Search parameters for Temyiz Kurulu decisions
|
||||
|
||||
Returns:
|
||||
TemyizKuruluSearchResponse with matching decisions
|
||||
"""
|
||||
# Initialize session if needed
|
||||
if 'temyiz_kurulu' not in self.csrf_tokens:
|
||||
if not await self._initialize_session_for_endpoint('temyiz_kurulu'):
|
||||
raise Exception("Failed to initialize session for Temyiz Kurulu endpoint")
|
||||
|
||||
form_data = self._build_temyiz_kurulu_form_data(params)
|
||||
encoded_data = urlencode(form_data, encoding='utf-8')
|
||||
|
||||
logger.info(f"Searching Temyiz Kurulu decisions with parameters: {params.model_dump(exclude_none=True)}")
|
||||
|
||||
try:
|
||||
# Update headers with cookies
|
||||
headers = self.http_client.headers.copy()
|
||||
if self.session_cookies:
|
||||
cookie_header = "; ".join([f"{k}={v}" for k, v in self.session_cookies.items()])
|
||||
headers["Cookie"] = cookie_header
|
||||
|
||||
response = await self.http_client.post(
|
||||
self.TEMYIZ_KURULU_ENDPOINT,
|
||||
data=encoded_data,
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
response_json = response.json()
|
||||
|
||||
# Parse response
|
||||
decisions = []
|
||||
for item in response_json.get('data', []):
|
||||
decisions.append(TemyizKuruluDecision(
|
||||
id=item['Id'],
|
||||
temyiz_tutanak_tarihi=item['TEMYIZTUTANAKTARIHI'],
|
||||
ilam_dairesi=item['ILAMDAIRESI'],
|
||||
temyiz_karar=item['TEMYIZKARAR']
|
||||
))
|
||||
|
||||
return TemyizKuruluSearchResponse(
|
||||
decisions=decisions,
|
||||
total_records=response_json.get('recordsTotal', 0),
|
||||
total_filtered=response_json.get('recordsFiltered', 0),
|
||||
draw=response_json.get('draw', 1)
|
||||
)
|
||||
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"HTTP error during Temyiz Kurulu search: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing Temyiz Kurulu search: {e}")
|
||||
raise
|
||||
|
||||
async def search_daire_decisions(self, params: DaireSearchRequest) -> DaireSearchResponse:
|
||||
"""
|
||||
Search Sayıştay Daire (Chamber) decisions.
|
||||
|
||||
Args:
|
||||
params: Search parameters for Daire decisions
|
||||
|
||||
Returns:
|
||||
DaireSearchResponse with matching decisions
|
||||
"""
|
||||
# Initialize session if needed
|
||||
if 'daire' not in self.csrf_tokens:
|
||||
if not await self._initialize_session_for_endpoint('daire'):
|
||||
raise Exception("Failed to initialize session for Daire endpoint")
|
||||
|
||||
form_data = self._build_daire_form_data(params)
|
||||
encoded_data = urlencode(form_data, encoding='utf-8')
|
||||
|
||||
logger.info(f"Searching Daire decisions with parameters: {params.model_dump(exclude_none=True)}")
|
||||
|
||||
try:
|
||||
# Update headers with cookies
|
||||
headers = self.http_client.headers.copy()
|
||||
if self.session_cookies:
|
||||
cookie_header = "; ".join([f"{k}={v}" for k, v in self.session_cookies.items()])
|
||||
headers["Cookie"] = cookie_header
|
||||
|
||||
response = await self.http_client.post(
|
||||
self.DAIRE_ENDPOINT,
|
||||
data=encoded_data,
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
response_json = response.json()
|
||||
|
||||
# Parse response
|
||||
decisions = []
|
||||
for item in response_json.get('data', []):
|
||||
decisions.append(DaireDecision(
|
||||
id=item['Id'],
|
||||
yargilama_dairesi=item['YARGILAMADAIRESI'],
|
||||
karar_tarih=item['KARARTRH'],
|
||||
karar_no=item['KARARNO'],
|
||||
ilam_no=item.get('ILAMNO'), # Use get() to handle None values
|
||||
madde_no=item['MADDENO'],
|
||||
kamu_idaresi_turu=item['KAMUIDARESITURU'],
|
||||
hesap_yili=item['HESAPYILI'],
|
||||
web_karar_konusu=item['WEBKARARKONUSU'],
|
||||
web_karar_metni=item['WEBKARARMETNI']
|
||||
))
|
||||
|
||||
return DaireSearchResponse(
|
||||
decisions=decisions,
|
||||
total_records=response_json.get('recordsTotal', 0),
|
||||
total_filtered=response_json.get('recordsFiltered', 0),
|
||||
draw=response_json.get('draw', 1)
|
||||
)
|
||||
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"HTTP error during Daire search: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing Daire search: {e}")
|
||||
raise
|
||||
|
||||
def _convert_html_to_markdown(self, html_content: str) -> Optional[str]:
|
||||
"""Convert HTML content to Markdown using MarkItDown with BytesIO to avoid filename length issues."""
|
||||
if not html_content:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Convert HTML string to bytes and create BytesIO stream
|
||||
html_bytes = html_content.encode('utf-8')
|
||||
html_stream = io.BytesIO(html_bytes)
|
||||
|
||||
# Pass BytesIO stream to MarkItDown to avoid temp file creation
|
||||
md_converter = MarkItDown()
|
||||
result = md_converter.convert(html_stream)
|
||||
markdown_content = result.text_content
|
||||
|
||||
logger.info("Successfully converted HTML to Markdown")
|
||||
return markdown_content
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error converting HTML to Markdown: {e}")
|
||||
return f"Error converting HTML content: {str(e)}"
|
||||
|
||||
async def get_document_as_markdown(self, decision_id: str, decision_type: str) -> SayistayDocumentMarkdown:
|
||||
"""
|
||||
Retrieve full text of a Sayıştay decision and convert to Markdown.
|
||||
|
||||
Args:
|
||||
decision_id: Unique decision identifier
|
||||
decision_type: Type of decision ('genel_kurul', 'temyiz_kurulu', 'daire')
|
||||
|
||||
Returns:
|
||||
SayistayDocumentMarkdown with converted content
|
||||
"""
|
||||
logger.info(f"Retrieving document for {decision_type} decision ID: {decision_id}")
|
||||
|
||||
# Validate decision_id
|
||||
if not decision_id or not decision_id.strip():
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url="",
|
||||
markdown_content=None,
|
||||
error_message="Decision ID cannot be empty"
|
||||
)
|
||||
|
||||
# Map decision type to URL path
|
||||
url_path_mapping = {
|
||||
'genel_kurul': 'KararlarGenelKurul',
|
||||
'temyiz_kurulu': 'KararlarTemyiz',
|
||||
'daire': 'KararlarDaire'
|
||||
}
|
||||
|
||||
if decision_type not in url_path_mapping:
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url="",
|
||||
markdown_content=None,
|
||||
error_message=f"Invalid decision type: {decision_type}. Must be one of: {list(url_path_mapping.keys())}"
|
||||
)
|
||||
|
||||
# Build document URL
|
||||
url_path = url_path_mapping[decision_type]
|
||||
document_url = f"{self.BASE_URL}/{url_path}/Detay/{decision_id}/"
|
||||
|
||||
try:
|
||||
# Make HTTP GET request to document URL
|
||||
headers = {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
|
||||
"Sec-Fetch-Dest": "document",
|
||||
"Sec-Fetch-Mode": "navigate",
|
||||
"Sec-Fetch-Site": "same-origin"
|
||||
}
|
||||
|
||||
# Include session cookies if available
|
||||
if self.session_cookies:
|
||||
cookie_header = "; ".join([f"{k}={v}" for k, v in self.session_cookies.items()])
|
||||
headers["Cookie"] = cookie_header
|
||||
|
||||
response = await self.http_client.get(document_url, headers=headers)
|
||||
response.raise_for_status()
|
||||
html_content = response.text
|
||||
|
||||
if not html_content or not html_content.strip():
|
||||
logger.warning(f"Received empty HTML content from {document_url}")
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url=document_url,
|
||||
markdown_content=None,
|
||||
error_message="Document content is empty"
|
||||
)
|
||||
|
||||
# Convert HTML to Markdown using existing method
|
||||
markdown_content = self._convert_html_to_markdown(html_content)
|
||||
|
||||
if markdown_content and "Error converting HTML content" not in markdown_content:
|
||||
logger.info(f"Successfully retrieved and converted document {decision_id} to Markdown")
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url=document_url,
|
||||
markdown_content=markdown_content,
|
||||
retrieval_date=None # Could add datetime.now().isoformat() if needed
|
||||
)
|
||||
else:
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url=document_url,
|
||||
markdown_content=None,
|
||||
error_message=f"Failed to convert HTML to Markdown: {markdown_content}"
|
||||
)
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
error_msg = f"HTTP error {e.response.status_code} when fetching document: {e}"
|
||||
logger.error(f"HTTP error fetching document {decision_id}: {error_msg}")
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url=document_url,
|
||||
markdown_content=None,
|
||||
error_message=error_msg
|
||||
)
|
||||
except httpx.RequestError as e:
|
||||
error_msg = f"Network error when fetching document: {e}"
|
||||
logger.error(f"Network error fetching document {decision_id}: {error_msg}")
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url=document_url,
|
||||
markdown_content=None,
|
||||
error_message=error_msg
|
||||
)
|
||||
except Exception as e:
|
||||
error_msg = f"Unexpected error when fetching document: {e}"
|
||||
logger.error(f"Unexpected error fetching document {decision_id}: {error_msg}")
|
||||
return SayistayDocumentMarkdown(
|
||||
decision_id=decision_id,
|
||||
decision_type=decision_type,
|
||||
source_url=document_url,
|
||||
markdown_content=None,
|
||||
error_message=error_msg
|
||||
)
|
||||
|
||||
async def close_client_session(self):
|
||||
"""Close HTTP client session."""
|
||||
if hasattr(self, 'http_client') and self.http_client and not self.http_client.is_closed:
|
||||
await self.http_client.aclose()
|
||||
logger.info("SayistayApiClient: HTTP client session closed.")
|
||||
@@ -0,0 +1,61 @@
|
||||
# sayistay_mcp_module/enums.py
|
||||
|
||||
from typing import Literal
|
||||
|
||||
# Chamber/Daire options for Temyiz Kurulu and Daire endpoints (1-8 + All)
|
||||
DaireEnum = Literal[
|
||||
"ALL", # All chambers/departments
|
||||
"1", # 1. Daire
|
||||
"2", # 2. Daire
|
||||
"3", # 3. Daire
|
||||
"4", # 4. Daire
|
||||
"5", # 5. Daire
|
||||
"6", # 6. Daire
|
||||
"7", # 7. Daire
|
||||
"8" # 8. Daire
|
||||
]
|
||||
|
||||
# Public Administration Types (Kamu İdaresi Türü)
|
||||
KamuIdaresiTuruEnum = Literal[
|
||||
"ALL", # All institutions
|
||||
"Genel Bütçe Kapsamındaki İdareler", # General Budget Administrations
|
||||
"Yüksek Öğretim Kurumları", # Higher Education Institutions
|
||||
"Diğer Özel Bütçeli İdareler", # Other Special Budget Administrations
|
||||
"Düzenleyici ve Denetleyici Kurumlar", # Regulatory and Supervisory Institutions
|
||||
"Sosyal Güvenlik Kurumları", # Social Security Institutions
|
||||
"Özel İdareler", # Special Administrations
|
||||
"Belediyeler ve Bağlı İdareler", # Municipalities and Affiliated Administrations
|
||||
"Diğer" # Other
|
||||
]
|
||||
|
||||
# Decision Subject Categories (Web Karar Konusu) - Shortened for token efficiency
|
||||
WebKararKonusuEnum = Literal[
|
||||
"ALL", # All subjects
|
||||
"Harcırah Mevzuatı", # Travel Allowance Legislation
|
||||
"İhale Mevzuatı", # Procurement Legislation
|
||||
"İş Mevzuatı", # Labor Legislation
|
||||
"Personel Mevzuatı", # Personnel Legislation
|
||||
"Sorumluluk ve Yargılama Usulleri", # Liability and Trial Procedures
|
||||
"Vergi Resmi Harç ve Diğer Gelirler", # Tax, Official Fee and Other Revenue
|
||||
"Çeşitli Konular" # Various Topics
|
||||
]
|
||||
|
||||
# Mapping from shortened enum values to full API values
|
||||
WEB_KARAR_KONUSU_MAPPING = {
|
||||
"ALL": "ALL",
|
||||
"Harcırah Mevzuatı": "Harcırah Mevzuatı ile İlgili Kararlar",
|
||||
"İhale Mevzuatı": "İhale Mevzuatı ile İlgili Kararlar",
|
||||
"İş Mevzuatı": "İş Mevzuatı ile İlgili Kararlar",
|
||||
"Personel Mevzuatı": "Personel Mevzuatı ile İlgili Kararlar",
|
||||
"Sorumluluk ve Yargılama Usulleri": "Sorumluluk ve Yargılama Usulleri ile İlgili Kararlar",
|
||||
"Vergi Resmi Harç ve Diğer Gelirler": "Vergi Resmi Harç ve Diğer Gelirlerle İlgili Kararlar",
|
||||
"Çeşitli Konular": "Çeşitli Konuları İlgilendiren Kararlar"
|
||||
}
|
||||
|
||||
# Year ranges for different endpoints
|
||||
GENEL_KURUL_YEARS = [str(year) for year in range(2006, 2025)] # 2006-2024
|
||||
TEMYIZ_KURULU_YEARS = [str(year) for year in range(1993, 2023)] # 1993-2022
|
||||
DAIRE_YEARS = [str(year) for year in range(2012, 2026)] # 2012-2025
|
||||
|
||||
# Account years for Temyiz Kurulu and Daire endpoints
|
||||
HESAP_YILLARI = [str(year) for year in range(1993, 2024)] # 1993-2023
|
||||
@@ -0,0 +1,220 @@
|
||||
# sayistay_mcp_module/models.py
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List, Union, Dict, Any, Literal
|
||||
from enum import Enum
|
||||
from .enums import DaireEnum, KamuIdaresiTuruEnum, WebKararKonusuEnum
|
||||
|
||||
# --- Unified Enums ---
|
||||
class SayistayDecisionTypeEnum(str, Enum):
|
||||
GENEL_KURUL = "genel_kurul"
|
||||
TEMYIZ_KURULU = "temyiz_kurulu"
|
||||
DAIRE = "daire"
|
||||
|
||||
# ============================================================================
|
||||
# Genel Kurul (General Assembly) Models
|
||||
# ============================================================================
|
||||
|
||||
class GenelKurulSearchRequest(BaseModel):
|
||||
"""
|
||||
Search request for Sayıştay Genel Kurul (General Assembly) decisions.
|
||||
|
||||
Genel Kurul decisions are precedent-setting rulings made by the full assembly
|
||||
of the Turkish Court of Accounts, typically addressing interpretation of
|
||||
audit and accountability regulations.
|
||||
"""
|
||||
karar_no: str = Field("", description="Decision no")
|
||||
karar_ek: str = Field("", description="Appendix no")
|
||||
|
||||
karar_tarih_baslangic: str = Field("", description="Start year (YYYY)")
|
||||
|
||||
karar_tarih_bitis: str = Field("", description="End year")
|
||||
|
||||
karar_tamami: str = Field("", description="Value")
|
||||
|
||||
# DataTables pagination
|
||||
start: int = Field(0, description="Starting record for pagination (0-based)")
|
||||
length: int = Field(10, description="Number of records per page (1-10)")
|
||||
|
||||
class GenelKurulDecision(BaseModel):
|
||||
"""Single Genel Kurul decision entry from search results."""
|
||||
id: int = Field(..., description="Unique decision ID")
|
||||
karar_no: str = Field(..., description="Decision number (e.g., '5415/1')")
|
||||
karar_tarih: str = Field(..., description="Decision date in DD.MM.YYYY format")
|
||||
karar_ozeti: str = Field(..., description="Decision summary/abstract")
|
||||
|
||||
class GenelKurulSearchResponse(BaseModel):
|
||||
"""Response from Genel Kurul search endpoint."""
|
||||
decisions: List[GenelKurulDecision] = Field(default_factory=list, description="List of matching decisions")
|
||||
total_records: int = Field(0, description="Total number of matching records")
|
||||
total_filtered: int = Field(0, description="Number of records after filtering")
|
||||
draw: int = Field(1, description="DataTables draw counter")
|
||||
|
||||
# ============================================================================
|
||||
# Temyiz Kurulu (Appeals Board) Models
|
||||
# ============================================================================
|
||||
|
||||
class TemyizKuruluSearchRequest(BaseModel):
|
||||
"""
|
||||
Search request for Sayıştay Temyiz Kurulu (Appeals Board) decisions.
|
||||
|
||||
Temyiz Kurulu reviews appeals against audit chamber decisions,
|
||||
providing higher-level review of audit findings and sanctions.
|
||||
"""
|
||||
ilam_dairesi: DaireEnum = Field("ALL", description="Value")
|
||||
|
||||
yili: str = Field("", description="Value")
|
||||
|
||||
karar_tarih_baslangic: str = Field("", description="Value")
|
||||
|
||||
karar_tarih_bitis: str = Field("", description="End year")
|
||||
|
||||
kamu_idaresi_turu: KamuIdaresiTuruEnum = Field("ALL", description="Value")
|
||||
|
||||
ilam_no: str = Field("", description="Audit report number (İlam No, max 50 chars)")
|
||||
dosya_no: str = Field("", description="File number for the case")
|
||||
temyiz_tutanak_no: str = Field("", description="Appeals board meeting minutes number")
|
||||
|
||||
temyiz_karar: str = Field("", description="Value")
|
||||
|
||||
web_karar_konusu: WebKararKonusuEnum = Field("ALL", description="Value")
|
||||
|
||||
# DataTables pagination
|
||||
start: int = Field(0, description="Starting record for pagination (0-based)")
|
||||
length: int = Field(10, description="Number of records per page (1-10)")
|
||||
|
||||
class TemyizKuruluDecision(BaseModel):
|
||||
"""Single Temyiz Kurulu decision entry from search results."""
|
||||
id: int = Field(..., description="Unique decision ID")
|
||||
temyiz_tutanak_tarihi: str = Field(..., description="Appeals board meeting date in DD.MM.YYYY format")
|
||||
ilam_dairesi: int = Field(..., description="Chamber number (1-8)")
|
||||
temyiz_karar: str = Field(..., description="Appeals decision summary and reasoning")
|
||||
|
||||
class TemyizKuruluSearchResponse(BaseModel):
|
||||
"""Response from Temyiz Kurulu search endpoint."""
|
||||
decisions: List[TemyizKuruluDecision] = Field(default_factory=list, description="List of matching appeals decisions")
|
||||
total_records: int = Field(0, description="Total number of matching records")
|
||||
total_filtered: int = Field(0, description="Number of records after filtering")
|
||||
draw: int = Field(1, description="DataTables draw counter")
|
||||
|
||||
# ============================================================================
|
||||
# Daire (Chamber) Models
|
||||
# ============================================================================
|
||||
|
||||
class DaireSearchRequest(BaseModel):
|
||||
"""
|
||||
Search request for Sayıştay Daire (Chamber) decisions.
|
||||
|
||||
Daire decisions are first-instance audit findings and sanctions
|
||||
issued by individual audit chambers before potential appeals.
|
||||
"""
|
||||
yargilama_dairesi: DaireEnum = Field("ALL", description="Value")
|
||||
|
||||
karar_tarih_baslangic: str = Field("", description="Value")
|
||||
|
||||
karar_tarih_bitis: str = Field("", description="End year")
|
||||
|
||||
ilam_no: str = Field("", description="Audit report number (İlam No, max 50 chars)")
|
||||
|
||||
kamu_idaresi_turu: KamuIdaresiTuruEnum = Field("ALL", description="Value")
|
||||
|
||||
hesap_yili: str = Field("", description="Value")
|
||||
|
||||
web_karar_konusu: WebKararKonusuEnum = Field("ALL", description="Value")
|
||||
|
||||
web_karar_metni: str = Field("", description="Value")
|
||||
|
||||
# DataTables pagination
|
||||
start: int = Field(0, description="Starting record for pagination (0-based)")
|
||||
length: int = Field(10, description="Number of records per page (1-10)")
|
||||
|
||||
class DaireDecision(BaseModel):
|
||||
"""Single Daire decision entry from search results."""
|
||||
id: int = Field(..., description="Unique decision ID")
|
||||
yargilama_dairesi: int = Field(..., description="Chamber number (1-8)")
|
||||
karar_tarih: str = Field(..., description="Decision date in DD.MM.YYYY format")
|
||||
karar_no: str = Field(..., description="Decision number")
|
||||
ilam_no: str = Field("", description="Audit report number (may be null)")
|
||||
madde_no: int = Field(..., description="Article/item number within the decision")
|
||||
kamu_idaresi_turu: str = Field(..., description="Public administration type")
|
||||
hesap_yili: int = Field(..., description="Account year being audited")
|
||||
web_karar_konusu: str = Field(..., description="Decision subject category")
|
||||
web_karar_metni: str = Field(..., description="Decision text/summary")
|
||||
|
||||
class DaireSearchResponse(BaseModel):
|
||||
"""Response from Daire search endpoint."""
|
||||
decisions: List[DaireDecision] = Field(default_factory=list, description="List of matching chamber decisions")
|
||||
total_records: int = Field(0, description="Total number of matching records")
|
||||
total_filtered: int = Field(0, description="Number of records after filtering")
|
||||
draw: int = Field(1, description="DataTables draw counter")
|
||||
|
||||
# ============================================================================
|
||||
# Document Models
|
||||
# ============================================================================
|
||||
|
||||
class SayistayDocumentMarkdown(BaseModel):
|
||||
"""
|
||||
Sayıştay decision document converted to Markdown format.
|
||||
|
||||
Used for retrieving full text of decisions from any of the three
|
||||
decision types (Genel Kurul, Temyiz Kurulu, Daire).
|
||||
"""
|
||||
decision_id: str = Field(..., description="Unique decision identifier")
|
||||
decision_type: str = Field(..., description="Value")
|
||||
source_url: str = Field(..., description="Original URL where the document was retrieved")
|
||||
markdown_content: Optional[str] = Field(None, description="Full decision text converted to Markdown format")
|
||||
retrieval_date: Optional[str] = Field(None, description="Date when document was retrieved (ISO format)")
|
||||
error_message: Optional[str] = Field(None, description="Error message if document retrieval failed")
|
||||
|
||||
# ============================================================================
|
||||
# Unified Models
|
||||
# ============================================================================
|
||||
|
||||
class SayistayUnifiedSearchRequest(BaseModel):
|
||||
"""Unified search request for all Sayıştay decision types."""
|
||||
decision_type: Literal["genel_kurul", "temyiz_kurulu", "daire"] = Field(..., description="Decision type: genel_kurul, temyiz_kurulu, or daire")
|
||||
|
||||
# Common pagination parameters
|
||||
start: int = Field(0, ge=0, description="Starting record for pagination (0-based)")
|
||||
length: int = Field(10, ge=1, le=100, description="Number of records per page (1-100)")
|
||||
|
||||
# Common search parameters
|
||||
karar_tarih_baslangic: str = Field("", description="Start date (DD.MM.YYYY format)")
|
||||
karar_tarih_bitis: str = Field("", description="End date (DD.MM.YYYY format)")
|
||||
kamu_idaresi_turu: KamuIdaresiTuruEnum = Field("ALL", description="Public administration type filter")
|
||||
ilam_no: str = Field("", description="Audit report number (İlam No, max 50 chars)")
|
||||
web_karar_konusu: WebKararKonusuEnum = Field("ALL", description="Decision subject category filter")
|
||||
|
||||
# Genel Kurul specific parameters (ignored for other types)
|
||||
karar_no: str = Field("", description="Decision number (genel_kurul only)")
|
||||
karar_ek: str = Field("", description="Decision appendix number (genel_kurul only)")
|
||||
karar_tamami: str = Field("", description="Full text search (genel_kurul only)")
|
||||
|
||||
# Temyiz Kurulu specific parameters (ignored for other types)
|
||||
ilam_dairesi: DaireEnum = Field("ALL", description="Audit chamber selection (temyiz_kurulu only)")
|
||||
yili: str = Field("", description="Year (YYYY format, temyiz_kurulu only)")
|
||||
dosya_no: str = Field("", description="File number (temyiz_kurulu only)")
|
||||
temyiz_tutanak_no: str = Field("", description="Appeals board meeting minutes number (temyiz_kurulu only)")
|
||||
temyiz_karar: str = Field("", description="Appeals decision text search (temyiz_kurulu only)")
|
||||
|
||||
# Daire specific parameters (ignored for other types)
|
||||
yargilama_dairesi: DaireEnum = Field("ALL", description="Chamber selection (daire only)")
|
||||
hesap_yili: str = Field("", description="Account year (daire only)")
|
||||
web_karar_metni: str = Field("", description="Decision text search (daire only)")
|
||||
|
||||
class SayistayUnifiedSearchResult(BaseModel):
|
||||
"""Unified search result containing decisions from any Sayıştay decision type."""
|
||||
decision_type: Literal["genel_kurul", "temyiz_kurulu", "daire"] = Field(..., description="Type of decisions returned")
|
||||
decisions: List[Dict[str, Any]] = Field(default_factory=list, description="Decision list (structure varies by type)")
|
||||
total_records: int = Field(0, description="Total number of records found")
|
||||
total_filtered: int = Field(0, description="Number of records after filtering")
|
||||
draw: int = Field(1, description="DataTables draw counter")
|
||||
|
||||
class SayistayUnifiedDocumentMarkdown(BaseModel):
|
||||
"""Unified document model for all Sayıştay decision types."""
|
||||
decision_type: Literal["genel_kurul", "temyiz_kurulu", "daire"] = Field(..., description="Type of document")
|
||||
decision_id: str = Field(..., description="Decision ID")
|
||||
source_url: str = Field(..., description="Source URL of the document")
|
||||
document_data: Dict[str, Any] = Field(default_factory=dict, description="Document content and metadata")
|
||||
markdown_content: Optional[str] = Field(None, description="Markdown content")
|
||||
error_message: Optional[str] = Field(None, description="Error message if retrieval failed")
|
||||
@@ -0,0 +1,133 @@
|
||||
# sayistay_mcp_module/unified_client.py
|
||||
# Unified client for all three Sayıştay decision types
|
||||
|
||||
import logging
|
||||
from typing import Optional, Dict, Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from .models import (
|
||||
SayistayUnifiedSearchRequest,
|
||||
SayistayUnifiedSearchResult,
|
||||
SayistayUnifiedDocumentMarkdown,
|
||||
GenelKurulSearchRequest,
|
||||
TemyizKuruluSearchRequest,
|
||||
DaireSearchRequest
|
||||
)
|
||||
from .client import SayistayApiClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class SayistayUnifiedClient:
|
||||
"""Unified client that handles all three Sayıştay decision types."""
|
||||
|
||||
def __init__(self, request_timeout: float = 60.0):
|
||||
self.client = SayistayApiClient(request_timeout)
|
||||
|
||||
async def search_unified(self, params: SayistayUnifiedSearchRequest) -> SayistayUnifiedSearchResult:
|
||||
"""Unified search that routes to appropriate search method based on decision_type."""
|
||||
|
||||
if params.decision_type == "genel_kurul":
|
||||
# Convert to genel kurul request
|
||||
genel_kurul_params = GenelKurulSearchRequest(
|
||||
karar_no=params.karar_no,
|
||||
karar_ek=params.karar_ek,
|
||||
karar_tarih_baslangic=params.karar_tarih_baslangic,
|
||||
karar_tarih_bitis=params.karar_tarih_bitis,
|
||||
karar_tamami=params.karar_tamami,
|
||||
start=params.start,
|
||||
length=params.length
|
||||
)
|
||||
|
||||
result = await self.client.search_genel_kurul_decisions(genel_kurul_params)
|
||||
|
||||
# Convert to unified format
|
||||
decisions_list = [decision.model_dump() for decision in result.decisions]
|
||||
|
||||
return SayistayUnifiedSearchResult(
|
||||
decision_type="genel_kurul",
|
||||
decisions=decisions_list,
|
||||
total_records=result.total_records,
|
||||
total_filtered=result.total_filtered,
|
||||
draw=result.draw
|
||||
)
|
||||
|
||||
elif params.decision_type == "temyiz_kurulu":
|
||||
# Convert to temyiz kurulu request
|
||||
temyiz_params = TemyizKuruluSearchRequest(
|
||||
ilam_dairesi=params.ilam_dairesi,
|
||||
yili=params.yili,
|
||||
karar_tarih_baslangic=params.karar_tarih_baslangic,
|
||||
karar_tarih_bitis=params.karar_tarih_bitis,
|
||||
kamu_idaresi_turu=params.kamu_idaresi_turu,
|
||||
ilam_no=params.ilam_no,
|
||||
dosya_no=params.dosya_no,
|
||||
temyiz_tutanak_no=params.temyiz_tutanak_no,
|
||||
temyiz_karar=params.temyiz_karar,
|
||||
web_karar_konusu=params.web_karar_konusu,
|
||||
start=params.start,
|
||||
length=params.length
|
||||
)
|
||||
|
||||
result = await self.client.search_temyiz_kurulu_decisions(temyiz_params)
|
||||
|
||||
# Convert to unified format
|
||||
decisions_list = [decision.model_dump() for decision in result.decisions]
|
||||
|
||||
return SayistayUnifiedSearchResult(
|
||||
decision_type="temyiz_kurulu",
|
||||
decisions=decisions_list,
|
||||
total_records=result.total_records,
|
||||
total_filtered=result.total_filtered,
|
||||
draw=result.draw
|
||||
)
|
||||
|
||||
elif params.decision_type == "daire":
|
||||
# Convert to daire request
|
||||
daire_params = DaireSearchRequest(
|
||||
yargilama_dairesi=params.yargilama_dairesi,
|
||||
karar_tarih_baslangic=params.karar_tarih_baslangic,
|
||||
karar_tarih_bitis=params.karar_tarih_bitis,
|
||||
ilam_no=params.ilam_no,
|
||||
kamu_idaresi_turu=params.kamu_idaresi_turu,
|
||||
hesap_yili=params.hesap_yili,
|
||||
web_karar_konusu=params.web_karar_konusu,
|
||||
web_karar_metni=params.web_karar_metni,
|
||||
start=params.start,
|
||||
length=params.length
|
||||
)
|
||||
|
||||
result = await self.client.search_daire_decisions(daire_params)
|
||||
|
||||
# Convert to unified format
|
||||
decisions_list = [decision.model_dump() for decision in result.decisions]
|
||||
|
||||
return SayistayUnifiedSearchResult(
|
||||
decision_type="daire",
|
||||
decisions=decisions_list,
|
||||
total_records=result.total_records,
|
||||
total_filtered=result.total_filtered,
|
||||
draw=result.draw
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unsupported decision type: {params.decision_type}")
|
||||
|
||||
async def get_document_unified(self, decision_id: str, decision_type: str) -> SayistayUnifiedDocumentMarkdown:
|
||||
"""Unified document retrieval for all Sayıştay decision types."""
|
||||
|
||||
# Use existing client method (decision_type is already a string)
|
||||
result = await self.client.get_document_as_markdown(decision_id, decision_type)
|
||||
|
||||
return SayistayUnifiedDocumentMarkdown(
|
||||
decision_type=decision_type,
|
||||
decision_id=result.decision_id,
|
||||
source_url=result.source_url,
|
||||
document_data=result.model_dump(),
|
||||
markdown_content=result.markdown_content,
|
||||
error_message=result.error_message
|
||||
)
|
||||
|
||||
async def close_client_session(self):
|
||||
"""Close the underlying client session."""
|
||||
if hasattr(self.client, 'close_client_session'):
|
||||
await self.client.close_client_session()
|
||||
Reference in New Issue
Block a user